Skip to content

PostgreSQL psql 常用命令 ​

本文介绍了在 PostgreSQL 提供的 psql 工具中的常用的命令。

psql 工具是 PostgreSQL 提供的一个客户端程序。您能使用 psql 工具管理 PostgreSQL 数据库服务器。 本文整理了常用的 psql 命令,以便您能够更加有效的管理 PostgreSQL 数据库服务器。

连接 PostgreSQL ​

要使用 psql 工具管理 PostgreSQL 服务器,请先连接到 PostgreSQL 服务器。 命令如下:

sql
psql -d dbname -U  user -W

如果您需要连接一个远程的 PostgreSQL 服务器,请使用如下命令:

sql
psql -h host -p port -d dbname -U  user -W

其中:

  • -h 参数用于指定远程 PostgreSQL 服务器的主机名或者 IP 地址。 默认值为 localhost。
  • -p 参数用于指定远程 PostgreSQL 服务器的端口号。默认值为 5432。

psql 常用命令 ​

当您使用 psql 登录进 PostgreSQL 服务器以后,您就可以使用下面的命令管理服务器了。

列出所有的数据库 ​

要列出当前 PostgreSQL 数据库服务器中的所有数据库,请使用 \l 或者 \l+ 命令:

sql
\l

或者

sql
\l+

连接到数据库 ​

连接数据库请使用 \c 或者 \connect 命令。

要使用当前用户连接到新的数据库,请使用如下命令:

sql
\c dbname

要使用新的用户连接到当前数据库,请使用如下命令:

sql
\c - username

您可以是用 \connect 替换上面命令中的 \c,他们是等效的。

列出数据库中的表 ​

要列出当前数据库中的表,请使用 \dt 或者 \dt+ 命令:

sql
\dt

或者

sql
\dt+

显示表结构 ​

要显示一个表的结构或定义,比如 列,约束等信息,请使用 \d 命令:

sql
\d table_name

比如,要查看 product 表的结构,请使用如下命令:

sql
\d product
bash
testdb=# \d product
                                 Table "public.product"
    Column    |       Type        | Collation | Nullable |           Default
--------------+-------------------+-----------+----------+------------------------------
 id           | integer           |           | not null | generated always as identity
 product_name | character varying |           | not null |
 attributes   | hstore            |           |          |
Indexes:
    "product_pkey" PRIMARY KEY, btree (id)

列出可用模式 ​

要列出当前连接的数据库的所有模式,请使用该 \dn 命令。

sql
\dn
bash
  List of schemas
  Name  |  Owner
--------+----------
 public | postgres

列出可用的函数 ​

要列出当前数据库中的可用函数,请使用该 \df 命令。

sql
\df
bash
                                                    List of functions
 Schema |           Name           |  Result data type  |                   Argument data types                   | Type
--------+--------------------------+--------------------+---------------------------------------------------------+------
 public | akeys                    | text[]             | hstore                                                  | func
 public | avals                    | text[]             | hstore                                                  | func
 public | defined                  | boolean            | hstore, text                                            | func
 public | delete                   | hstore             | hstore, hstore                                          | func
 public | delete                   | hstore             | hstore, text                                            | func
 public | delete                   | hstore             | hstore, text[]                                          | func
 public | each                     | SETOF record       | hs hstore, OUT key text, OUT value text                 | func
 public | exist                    | boolean            | hstore, text                                            | func
 public | exists_all               | boolean            | hstore, text[]                                          | func
 public | exists_any               | boolean            | hstore, text[]                                          | func
 public | fetchval                 | text               | hstore, text                                            | func
 public | ghstore_compress         | internal           | internal                                                | func
 public | ghstore_consistent       | boolean            | internal, hstore, smallint, oid, internal               | func
 public | ghstore_decompress       | internal           | internal                                                | func
 public | ghstore_in               | ghstore            | cstring                                                 | func
 public | ghstore_options          | void               | internal                                                | func
 public | ghstore_out              | cstring            | ghstore                                                 | func
 public | ghstore_penalty          | internal           | internal, internal, internal                            | func
 public | ghstore_picksplit        | internal           | internal, internal                                      | func
 public | ghstore_same             | internal           | ghstore, ghstore, internal                              | func
 public | ghstore_union            | ghstore            | internal, internal                                      | func
 public | gin_consistent_hstore    | boolean            | internal, smallint, hstore, integer, internal, internal | func
 public | gin_extract_hstore       | internal           | hstore, internal                                        | func
 public | gin_extract_hstore_query | internal           | hstore, internal, smallint, internal, internal          | func
 public | hs_concat                | hstore             | hstore, hstore                                          | func
 public | hs_contained             | boolean            | hstore, hstore                                          | func
 public | hs_contains              | boolean            | hstore, hstore                                          | func
 public | hstore                   | hstore             | record                                                  | func
 public | hstore                   | hstore             | text, text                                              | func
 public | hstore                   | hstore             | text[]                                                  | func
 public | hstore                   | hstore             | text[], text[]                                          | func
 public | hstore_cmp               | integer            | hstore, hstore                                          | func
 public | hstore_eq                | boolean            | hstore, hstore                                          | func
 public | hstore_ge                | boolean            | hstore, hstore                                          | func
 public | hstore_gt                | boolean            | hstore, hstore                                          | func
 public | hstore_hash              | integer            | hstore                                                  | func
 public | hstore_hash_extended     | bigint             | hstore, bigint                                          | func
 public | hstore_in                | hstore             | cstring                                                 | func
 public | hstore_le                | boolean            | hstore, hstore                                          | func
 public | hstore_lt                | boolean            | hstore, hstore                                          | func
 public | hstore_ne                | boolean            | hstore, hstore                                          | func
 public | hstore_out               | cstring            | hstore                                                  | func
 public | hstore_recv              | hstore             | internal                                                | func
 public | hstore_send              | bytea              | hstore                                                  | func
 public | hstore_subscript_handler | internal           | internal                                                | func
 public | hstore_to_array          | text[]             | hstore                                                  | func
 public | hstore_to_json           | json               | hstore                                                  | func
 public | hstore_to_json_loose     | json               | hstore                                                  | func
 public | hstore_to_jsonb          | jsonb              | hstore                                                  | func
 public | hstore_to_jsonb_loose    | jsonb              | hstore                                                  | func
 public | hstore_to_matrix         | text[]             | hstore                                                  | func
 public | hstore_version_diag      | integer            | hstore                                                  | func
 public | isdefined                | boolean            | hstore, text                                            | func
 public | isexists                 | boolean            | hstore, text                                            | func
 public | my_time_multirange       | my_time_multirange |                                                         | func
 public | my_time_multirange       | my_time_multirange | VARIADIC my_time_range[]                                | func
 public | my_time_multirange       | my_time_multirange | my_time_range                                           | func
 public | my_time_range            | my_time_range      | time without time zone, time without time zone          | func
 public | my_time_range            | my_time_range      | time without time zone, time without time zone, text    | func
 public | populate_record          | anyelement         | anyelement, hstore                                      | func
 public | skeys                    | SETOF text         | hstore                                                  | func
 public | slice                    | hstore             | hstore, text[]                                          | func
 public | slice_array              | text[]             | hstore, text[]                                          | func
 public | svals                    | SETOF text         | hstore                                                  | func
 public | tconvert                 | hstore             | text, text                                              | func
(65 rows)

列出可用视图 ​

要列出当前数据库中的可用视图,请使用该 \dv 命令。

sql
\dv

列出用户及其角色 ​

要列出所有用户及其分配的角色,请使用 \du 命令:

sql
\du
bash
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

开启查询执行时间 ​

要打开查询执行时间,请使用该 \timing 命令。

sql
\timing
select * from product;
bash
 id | product_name |                          attributes
----+--------------+--------------------------------------------------------------
  2 | Shirt B      | "Color"=>"White", "Style"=>"Business", "Season"=>"Spring"
  1 | Computer A   | "CPU"=>"2.5", "Disk"=>"1T", "Brand"=>"Dell", "Memory"=>"16G"
(2 rows)

Time: 0.281 ms

当您再次运行 \timing 命令,则会关闭查询执行时间。

查看命令历史 ​

要显示命令历史记录,请使用该 \s 命令。

sql
\s

如果要将命令历史保存到文件中,则需要在 \s 命令后指定文件名 ,如下所示:

sql
\s filename

执行上一条命令 ​

要想执行最近的一条命令, 请使用 \g 命令:

sql
\g

\g 可让你避免重新输入上一条命令。

获取 SQL 命令的帮助 ​

要获取 SQL 命令的说明,请使用 \h 命令,如下:

sql
\h sql_command

比如,要获取 TRUNCATE 的帮助说明,请使用如下的命令:

sql
\h TRUNCATE
bash
Command:     TRUNCATE
Description: empty a table or set of tables
Syntax:
TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
    [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]

URL: https://www.postgresql.org/docs/14/sql-truncate.html

获取 psql 的帮助 ​

要了解 psql 命令的详细用法,请使用 \? 命令

sql
\?

从文件中执行 psql 命令 ​

如果要从文件执行 psql 命令,请 \i 按如下方式使用命令:

sql
\i filename

打开扩展显示 ​

要为 SELECT 语句的结果集打开扩展显示,请使用 \x 命令。

sql
\x
select * from product;
bash
-[ RECORD 1 ]+-------------------------------------------------------------
id           | 2
product_name | Shirt B
attributes   | "Color"=>"White", "Style"=>"Business", "Season"=>"Spring"
-[ RECORD 2 ]+-------------------------------------------------------------
id           | 1
product_name | Computer A
attributes   | "CPU"=>"2.5", "Disk"=>"1T", "Brand"=>"Dell", "Memory"=>"16G"

扩展显示对于显示那些很长的列很有帮助。

如果您再次运行 \x 命令。则回关闭扩展显示。

退出 psql ​

要退出 psql,您可以使用 \q 命令并按下 enter 退出 psql。

sql
\q

结论 ​

本文向您展示了 psql 工具的常用的命令。