文章目录

  • 前言
  • 1. 常用命令
  • 1.1 show databases;
  • 1.2 use xxxx;
  • 1.3 show tables & show create
  • 1.4 查询耗时
  • 1.5 show grants
  • 1.6 set names
  • 1.7 \G
  • 1.8 source
  • 1.9 edit
  • 1.10 查参数
  • 2. 快速查询帮助
  • 2.1 MySQL
  • 2.2 PostgreSQL


前言

PSQL 是 PostgreSQL 自带的命令行客户端,就好比 MySQL 的 mysql -h -p 客户端一样,不经常用,怕忘记,在此对比 MySQL 记录下。

1. 常用命令

1.1 show databases;

列出所有的数据库:\l

db1-# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 db1       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 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
(4 rows)

db1-#

1.2 use xxxx;

选择数据库:\c xxx

db1-# \c db1;
You are now connected to database "db1" as user "postgres".

1.3 show tables & show create

查询结构信息,PG 这里使用 \d 可以查看 表、视图、索引、序列 的信息。使用 \d+ 可以拿到更详细信息。

db1-# \d
              List of relations
 Schema |       Name       | Type  |  Owner   
--------+------------------+-------+----------
 public | pgbench_accounts | table | postgres
 public | pgbench_branches | table | postgres
 public | pgbench_history  | table | postgres
 public | pgbench_tellers  | table | postgres
(4 rows)

db1-# \d pgbench_accounts
              Table "public.pgbench_accounts"
  Column  |     Type      | Collation | Nullable | Default 
----------+---------------+-----------+----------+---------
 aid      | integer       |           | not null | 
 bid      | integer       |           |          | 
 abalance | integer       |           |          | 
 filler   | character(84) |           |          | 
Indexes:
    "pgbench_accounts_pkey" PRIMARY KEY, btree (aid)

db1-# \d pgbench_accounts_pkey
 Index "public.pgbench_accounts_pkey"
 Column |  Type   | Key? | Definition 
--------+---------+------+------------
 aid    | integer | yes  | aid
primary key, btree, for table "public.pgbench_accounts"

如果只想显示匹配的表,可以使用 “\dt” 命令。
如果只想显示索引,可以使用 “\di” 命令。
如果只想显示序列,可以使用 “\ds” 命令。
如果只想显示视图,可以使用 “\dv” 命令。
如果想显示函数,可以使用 “\df” 命令。

1.4 查询耗时

PSQL 默认执行 SQL 时不会显示耗时,可以使用命令开启:

db1=# select count(*) from pgbench_accounts;
  count  
---------
 5000000
(1 row)
db1=# \timing on
Timing is on.
db1=# 
db1=# 
db1=# select count(*) from pgbench_accounts;
  count  
---------
 5000000
(1 row)

Time: 212.943 ms

1.5 show grants

使用 \dg 或者 \du 可以查询用户信息

db1=# \dg
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}


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

1.6 set names

db1=# \encoding utf8;

1.7 \G

有时查询字段很多,几乎看不清,我们使用 \G 可以行转列

db1=# select * from pgbench_accounts limit 4;
 aid | bid | abalance |                                        filler                                        
-----+-----+----------+--------------------------------------------------------------------------------------
   1 |   1 |        0 |                                                                                     
   2 |   1 |        0 |                                                                                     
   3 |   1 |        0 |                                                                                     
   4 |   1 |        0 |                                                                                     
(4 rows)

Time: 0.479 ms
db1=# \x
Expanded display is on.
db1=# select * from pgbench_accounts limit 4;
-[ RECORD 1 ]----------------------------------------------------------------------------------
aid      | 1
bid      | 1
abalance | 0
filler   |                                                                                     
-[ RECORD 2 ]----------------------------------------------------------------------------------
aid      | 2
bid      | 1
abalance | 0
filler   |                                                                                     
-[ RECORD 3 ]----------------------------------------------------------------------------------
aid      | 3
bid      | 1
abalance | 0
filler   |                                                                                     
-[ RECORD 4 ]----------------------------------------------------------------------------------
aid      | 4
bid      | 1
abalance | 0
filler   |                                                                                     

Time: 0.545 ms
db1=#

1.8 source

MySQL 中 source 可以执行外部 SQL 文件 PG 可以使用 \i 文件名 执行外部 SQL 命令。
也可以使用 psql -f 文件名 执行外部 SQL 文件。

1.9 edit

MySQL 可以使用 eidt 进入编辑模式,PG 使用 \e 即可。

1.10 查参数

MySQL 中使用 show variables like '%max%' 可以模糊查询参数:

root@mysql 11:09:  [(none)]>show variables like '%max_c%';
+---------------------------------------+-------+
| Variable_name                         | Value |
+---------------------------------------+-------+
| max_connect_errors                    | 100   |
| max_connections                       | 1000  |
| performance_schema_max_cond_classes   | 80    |
| performance_schema_max_cond_instances | -1    |
+---------------------------------------+-------+

PostgreSQL 模糊查询参数的方法:

select name, setting from pg_settings where name like '%max_c%';

这张表的参数含义:

name:运行时配置参数名
setting:参数的当前值
unit:存储参数的单位,如ms
category:参数的逻辑组
short_desc:参数的简短描述
extra_desc:附加的参数的详细描述
context:用于记录参数的类型,可根据该值判断参数修改是否需重启生效
vartype:参数类型 (bool, enum, integer, real, or string)
source:当前参数值的来源
min_val:参数的最小允许值(对非数字值为空)
max_val:参数的最大允许值(对非数字值为空)
enumvals:用于存储参数的可取值(对非数字值为空)
boot_val:如果参数没有被别的其他设置,此列为在服务器启动时设定的参数值
reset_val:在当前会话中,RESET将会设置的参数值
sourcefile:当前值被设置的配置文件(空值表示从非配置文件的其他来源设置,由不是超级用户也不是pg_read_all_settings成员的用户检查时也为空值),在配置文件中使用include指令时有用
sourceline:当前值被设置的配置文件中的行号(空值表示从非配置文件的其他来源设置,由不是超级用户也不是pg_read_all_settings成员的用户检查时也为空值)。
pending_restart:如果配置文件中修改了该值但需要重启,则为true,否则为false

知道参数名,想快速查询到值,MySQL 可以使用 select @@参数名:

root@mysql 11:11:  [(none)]>select @@innodb_buffer_pool_size;
+---------------------------+
| @@innodb_buffer_pool_size |
+---------------------------+
|                2147483648 |
+---------------------------+

PostgreSQL 的查询方法是 show 参数名:

test01=# show shared_buffers;
 shared_buffers 
----------------
 128MB
(1 row)

2. 快速查询帮助

2.1 MySQL

MySQL 客户端中,使用 ? contents 命令可以输出帮助目录:

root@mysql 10:46:  [(none)]>? contents
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
   Account Management
   Administration
   Compound Statements
   Contents
   Data Definition
   Data Manipulation
   Data Types
   Functions
   Geographic Features
   Help Metadata
   Language Structure
   Plugins
   Procedures
   Storage Engines
   Table Maintenance
   Transactions
   User-Defined Functions
   Utility

例如查询 MySQL 数据类型,可以继续使用 ? Data Types 查询相关信息:

root@mysql 10:49:  [(none)]>? AUTO_INCREMENT;
Name: 'AUTO_INCREMENT'
Description:
The AUTO_INCREMENT attribute can be used to generate a unique identity
for new rows:

URL: https://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html

Examples:
CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
);

INSERT INTO animals (name) VALUES
    ('dog'),('cat'),('penguin'),
    ('lax'),('whale'),('ostrich');

SELECT * FROM animals;

这里还列出官方文档的地址,非常实用。除了按照层次查看帮助,还可以快速查询命令的用法,例如查询 drop 的用法:

root@mysql 10:50:  [(none)]>? drop
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
   ALTER TABLE
   ALTER TABLESPACE
   DEALLOCATE PREPARE
   DROP DATABASE
   DROP EVENT
   DROP FUNCTION
   DROP FUNCTION UDF
   DROP INDEX
   DROP PREPARE
   DROP PROCEDURE
   DROP SCHEMA
   DROP SERVER
   DROP TABLE
   DROP TABLESPACE
   DROP TRIGGER
   DROP USER
   DROP VIEW

2.2 PostgreSQL

PG 直接输入 \h 即可查询帮助目录:

PostgreSQL和mysql命令 POSTGRESQL和mysql命令的区别_postgresql


接下来根据目录选择查询详细内容,例如查询 \h DROP DATABASE 的用法:

test01=# \h DROP DATABASE
Command:     DROP DATABASE
Description: remove a database
Syntax:
DROP DATABASE [ IF EXISTS ] name [ [ WITH ] ( option [, ...] ) ]

where option can be:

    FORCE

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