PG客户端常用命令

由于PG客户端PGSQL不太支持很多SQL命令,比如DESC!
PG使用元命令来替代,注意元命令是反斜杠开始"\"

SHOW DATABASES   使用\l 小写的L

SHARKSQL> \l                                                  List of databases+-----------+-------+----------+-----------------+------------+------------+--------+-----------+-------------------+|   Name    | Owner | Encoding | Locale Provider |  Collate   |   Ctype    | Locale | ICU Rules | Access privileges |+-----------+-------+----------+-----------------+------------+------------+--------+-----------+-------------------+| postgres  | shark | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           |                   || template0 | shark | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =c/shark         +||           |       |          |                 |            |            |        |           | shark=CTc/shark   || template1 | shark | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =c/shark         +||           |       |          |                 |            |            |        |           | shark=CTc/shark   || testdb    | shark | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =Tc/shark        +||           |       |          |                 |            |            |        |           | shark=CTc/shark   |+-----------+-------+----------+-----------------+------------+------------+--------+-----------+-------------------+(4 rows

  

SHOW TABLES

查看当前数据库下所有表

SHARKSQL> \d         List of relations+--------+--------+-------+-------+| Schema |  Name  | Type  | Owner |+--------+--------+-------+-------+| public | mytest | table | shark |+--------+--------+-------+-------+(1 row)

查看表结构  DESC MYTEST

SHARKSQL> \d mytest                       Table "public.mytest"+--------+-----------------------+-----------+----------+---------+| Column |         Type          | Collation | Nullable | Default |+--------+-----------------------+-----------+----------+---------+| id     | integer               |           | not null |         || name   | character varying(30) |           |          |         |+--------+-----------------------+-----------+----------+---------+Indexes:    "mytest_pkey" PRIMARY KEY, btree (id)

切换数据库

 USER TESTDB

SHARKSQL> \c postgresYou are now connected to database "postgres" as user "shark".

查看当前链接信息,或者是当前数据库

SHARKSQL> \conninfo You are connected to database "testdb" as user "shark" via socket in "/tmp" at port "5432".SHARKSQL> \connectYou are now connected to database "testdb" as user "shark".

垂直显示记录

 SELECT * FROM MYTEST\G ;​​​​​​​

SHARKSQL> \xExpanded display is on.SHARKSQL> select * from mytest;[ RECORD 1 ]-| id   | 1       || name | Jack    |[ RECORD 2 ]-| id   | 2       || name | Tonny   |[ RECORD 3 ]-| id   | 3       || name | Acebaby |----+--------
Time: 3.782 msSHARKSQL> \xExpanded display is off.

执行OS命令:​​​​​​​

SHARKSQL> \! lsbooks   pg_shark.sql  scripts  source         update_rc.trace    update_rc.trace.old  模板  图片  下载  桌面Desktop  projects      soft  Untitled.rcnb  update_rc.trace.2  公共           视频  文档  音乐

注意\! 命令之间有空格

显示表更多信息

​​​​​​​SHARKSQL> \dS+ mytest                                                  Table "public.mytest"+--------+-----------------------+-----------+----------+---------+----------+-------------+--------------+-------------+| Column |         Type          | Collation | Nullable | Default | Storage  | Compression | Stats target | Description |+--------+-----------------------+-----------+----------+---------+----------+-------------+--------------+-------------+| id     | integer               |           | not null |         | plain    |             |              |             || name   | character varying(30) |           |          |         | extended |             |              |             |+--------+-----------------------+-----------+----------+---------+----------+-------------+--------------+-------------+Indexes:    "mytest_pkey" PRIMARY KEY, btree (id)Access method: heap

\dS+ 注意S大写

查看权限:​​​​​​​

SHARKSQL> \du                              List of roles+-----------+------------------------------------------------------------+| Role name |                         Attributes                         |+-----------+------------------------------------------------------------+| shark     | Superuser, Create role, Create DB, Replication, Bypass RLS || shark?     |                                                            |+-----------+------------------------------------------------------------+

显示元命令的SQL

SHARKSQL> \set ECHO_HIDDEN on
SHARKSQL> \l/******** QUERY *********/SELECT  d.datname as "Name",  pg_catalog.pg_get_userbyid(d.datdba) as "Owner",  pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",  CASE d.datlocprovider WHEN 'b' THEN 'builtin' WHEN 'c' THEN 'libc' WHEN 'i' THEN 'icu' END AS "Locale Provider",  d.datcollate as "Collate",  d.datctype as "Ctype",  d.datlocale as "Locale",  d.daticurules as "ICU Rules",  CASE WHEN pg_catalog.array_length(d.datacl, 1) = 0 THEN '(none)' ELSE pg_catalog.array_to_string(d.datacl, E'\n') END AS "Access privileges"FROM pg_catalog.pg_database dORDER BY 1;/************************/
                                                  List of databases+-----------+-------+----------+-----------------+------------+------------+--------+-----------+-------------------+|   Name    | Owner | Encoding | Locale Provider |  Collate   |   Ctype    | Locale | ICU Rules | Access privileges |+-----------+-------+----------+-----------------+------------+------------+--------+-----------+-------------------+| postgres  | shark | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           |                   || template0 | shark | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =c/shark         +||           |       |          |                 |            |            |        |           | shark=CTc/shark   || template1 | shark | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =c/shark         +||           |       |          |                 |            |            |        |           | shark=CTc/shark   || testdb    | shark | UTF8     | libc            | en_US.utf8 | en_US.utf8 |        |           | =Tc/shark        +||           |       |          |                 |            |            |        |           | shark=CTc/shark   |+-----------+-------+----------+-----------------+------------+------------+--------+-----------+-------------------+(4 rows)
SHARKSQL>

PGSQL帮助

图片

图片

PGSQL元命令其它

图片

图片

图片

图片

图片

相关文章:

PG17源码编译安装

PG 源码编译安装

PG17外链接​​​​​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值