psql的使用
psql时postgreSQL的命令行工具,提供了很多sql语法的简写,使用起来更加的方便.
psql的使用模式
psql包括两种使用模式:
交互模式
# 输入psql进入交互环境 [postgres@postgresql ~]$ psql psql (12.1, server 12.7) Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) postgres=# exit
非交互模式
# 在linux的命令行种直接执行命令 [postgres@postgresql ~]$ psql -l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) [postgres@postgresql ~]$
常用的命令
....
psql的使用技巧
历史命令:使用上下键来查看历史命令
命令补全: 使用tab键
取消自动提交的功能:
方法1:语句前写
begin;
提交或者回滚时,使用"commit;"
或者"rollback"
方法2: 关闭自动提交功能.
\set AUTOCOMMIT off
显示psql的命令对应的语句
方法1: 启动时加
-E
参数.方法2: 使用语句
\set ECHO_HIDDEN on | off
License:
CC BY 4.0