0.获得帮助

postgres=# \?
General
  \copyright             show PostgreSQL usage and distribution terms
  \crosstabview [COLUMNS] execute query and display result in crosstab
  \errverbose            show most recent error message at maximum verbosity
  \g [(OPTIONS)] [FILE]  execute query (and send result to file or |pipe);
                         \g with no arguments is equivalent to a semicolon
  \gdesc                 describe result of query, without executing it
  \gexec                 execute query, then execute each value in its result
  \gset [PREFIX]         execute query and store result in psql variables
  \gx [(OPTIONS)] [FILE] as \g, but forces expanded output mode
  \q                     quit psql
  \watch [SEC]           execute query every SEC seconds

Help
  \? [commands]          show help on backslash commands
  \? options             show help on psql command-line options
  \? variables           show help on special variables
  \h [NAME]              help on syntax of SQL commands, * for all commands

Query Buffer
  \e [FILE] [LINE]       edit the query buffer (or file) with external editor
  \ef [FUNCNAME [LINE]]  edit function definition with external editor
...

Informational
  (options: S = show system objects, + = additional detail)
  \d[S+]                 list tables, views, and sequences
  \d[S+]  NAME           describe table, view, sequence, or index
  \da[S]  [PATTERN]      list aggregates
  \dA[+]  [PATTERN]      list access methods

1.退出

postgres=# \q

2.列出了数据库的表、视图和序列

postgres=# \d
                  List of relations
Schema |          Name           | Type  |  Owner
-------+-------------------------+-------+----------
public | example_tbl             | table | postgres
public | pg_stat_statements      | view  | postgres
public | pg_stat_statements_info | view  | postgres
...

postgres=# \d+
                                              List of relations
Schema |          Name           | Type  |  Owner   | Persistence | Access method |    Size    | Description
-------+-------------------------+-------+----------+-------------+---------------+------------+-------------
public | example_tbl             | table | postgres | permanent   | heap          | 8192 bytes |
public | pg_stat_statements      | view  | postgres | permanent   |               | 0 bytes    |
public | pg_stat_statements_info | view  | postgres | permanent   |               | 0 bytes    |

postgres=# \dS+
                                               List of relations
   Schema   |              Name               | Type  |  Owner   | Persistence | Access method |    Size    | Description
------------+---------------------------------+-------+----------+-------------+---------------+------------+-------------
pg_catalog | pg_aggregate                    | table | postgres | permanent   | heap          | 56 kB      |
pg_catalog | pg_am                           | table | postgres | permanent   | heap          | 40 kB      |
pg_catalog | pg_amop                         | table | postgres | permanent   | heap          | 88 kB      |
pg_catalog | pg_amproc                       | table | postgres | permanent   | heap          | 72 kB      |
pg_catalog | pg_attrdef                      | table | postgres | permanent   | heap          | 8192 bytes |
...
public     | example_tbl                     | table | postgres | permanent   | heap          | 8192 bytes |
public     | pg_stat_statements              | view  | postgres | permanent   |               | 0 bytes    |
public     | pg_stat_statements_info         | view  | postgres | permanent   |

3.数据库操作

3.1切换数据库

postgres=# \c flywaytest

psql (15.0 (Ubuntu 15.0-1.pgdg20.04+1), server 15.1 (Debian 15.1-1.pgdg110+1))
You are now connected to database "flywaytest" as user "postgres".

3.2列出所有数据库

postgres=# \l+

4.显示对象详细信息

4.1显示表、视图和序列对象

postgres=# \d
                  List of relations
Schema |          Name           | Type  |  Owner
-------+-------------------------+-------+----------
public | example_tbl             | table | postgres
public | pg_stat_statements      | view  | postgres
public | pg_stat_statements_info | view  | postgres

4.2描述表、视图或序列的详细信息

postgres=# \d+ example_tbl
                                       Table "public.example_tbl"
Column |  Type   | Collation | Nullable | Default | Storage  | Compression | Stats target | Description |
-------+---------+-----------+----------+---------+----------+-------------+------------+-------------+
id     | integer |           | not null |         | plain    |             |              |
notes  | text    |           |          |         | extended |             |              |
Indexes:
    "idx_example_notes" btree (notes)
Access method: heap

4.3显示特定对象类型

参数

描述

E

外部表

I

索引

m

视图

s

序列

t

表格

v

视图

postgres=# \dt
            List of relations
 Schema |    Name     | Type  |  Owner
--------+-------------+-------+----------
 public | example_tbl | table | postgres

4.4显示角色和成员

postgres=# \du
                                  List of roles
Role name |                         Attributes                         |     Member of
----------+------------------------------------------------------------+---------
dev1      |                                                            | {devgrp}
dev2      |                                                            | {devgrp}
devgrp    | Cannot login                                               | {}
postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
read_only | Cannot login                                               | {pg_read_all_data}
rptusr  

4.5显示已安装的扩展

postgres=# \dx

4.6外部执行sql

sql -E postgresql://[username]:[password]@[hostname]:[port]/[database name]