psql 快捷键
1. 帮助
\h
testdb=# \h
Available help:
ABORT ALTER SEQUENCE CREATE AGGREGATE CREATE SUBSCRIPTION DROP EXTENSION DROP TEXT SEARCH PARSER RESET
ALTER AGGREGATE ALTER SERVER CREATE CAST CREATE TABLE DROP FOREIGN DATA WRAPPER DROP TEXT SEARCH TEMPLATE REVOKE
ALTER COLLATION ALTER STATISTICS CREATE COLLATION CREATE TABLE AS DROP FOREIGN TABLE DROP TRANSFORM ROLLBACK
ALTER CONVERSION ALTER SUBSCRIPTION CREATE CONVERSION CREATE TABLESPACE DROP FUNCTION DROP TRIGGER ROLLBACK PREPARED
ALTER DATABASE ALTER SYSTEM CREATE DATABASE CREATE TEXT SEARCH CONFIGURATION DROP GROUP DROP TYPE ROLLBACK TO SAVEPOINT
ALTER DEFAULT PRIVILEGES ALTER TABLE CREATE DOMAIN CREATE TEXT SEARCH DICTIONARY DROP INDEX DROP USER SAVEPOINT
ALTER DOMAIN ALTER TABLESPACE CREATE EVENT TRIGGER CREATE TEXT SEARCH PARSER DROP LANGUAGE DROP USER MAPPING SECURITY LABEL
ALTER EVENT TRIGGER ALTER TEXT SEARCH CONFIGURATION CREATE EXTENSION CREATE TEXT SEARCH TEMPLATE DROP MATERIALIZED VIEW DROP VIEW SELECT
ALTER EXTENSION ALTER TEXT SEARCH DICTIONARY CREATE FOREIGN DATA WRAPPER CREATE TRANSFORM DROP OPERATOR END SELECT INTO
ALTER FOREIGN DATA WRAPPER ALTER TEXT SEARCH PARSER CREATE FOREIGN TABLE CREATE TRIGGER DROP OPERATOR CLASS EXECUTE SET
ALTER FOREIGN TABLE ALTER TEXT SEARCH TEMPLATE CREATE FUNCTION CREATE TYPE DROP OPERATOR FAMILY EXPLAIN SET CONSTRAINTS
ALTER FUNCTION ALTER TRIGGER CREATE GROUP CREATE USER DROP OWNED FETCH SET ROLE
ALTER GROUP ALTER TYPE CREATE INDEX CREATE USER MAPPING DROP POLICY GRANT SET SESSION AUTHORIZATION
ALTER INDEX ALTER USER CREATE LANGUAGE CREATE VIEW DROP PROCEDURE IMPORT FOREIGN SCHEMA SET TRANSACTION
ALTER LANGUAGE ALTER USER MAPPING CREATE MATERIALIZED VIEW DEALLOCATE DROP PUBLICATION INSERT SHOW
ALTER LARGE OBJECT ALTER VIEW CREATE OPERATOR DECLARE DROP ROLE LISTEN START TRANSACTION
ALTER MATERIALIZED VIEW ANALYZE CREATE OPERATOR CLASS DELETE DROP ROUTINE LOAD TABLE
ALTER OPERATOR BEGIN CREATE OPERATOR FAMILY DISCARD DROP RULE LOCK TRUNCATE
ALTER OPERATOR CLASS CALL CREATE POLICY DO DROP SCHEMA MERGE UNLISTEN
ALTER OPERATOR FAMILY CHECKPOINT CREATE PROCEDURE DROP ACCESS METHOD DROP SEQUENCE MOVE UPDATE
ALTER POLICY CLOSE CREATE PUBLICATION DROP AGGREGATE DROP SERVER NOTIFY VACUUM
ALTER PROCEDURE CLUSTER CREATE ROLE DROP CAST DROP STATISTICS PREPARE VALUES
ALTER PUBLICATION COMMENT CREATE RULE DROP COLLATION DROP SUBSCRIPTION PREPARE TRANSACTION WITH
ALTER ROLE COMMIT CREATE SCHEMA DROP CONVERSION DROP TABLE REASSIGN OWNED
ALTER ROUTINE COMMIT PREPARED CREATE SEQUENCE DROP DATABASE DROP TABLESPACE REFRESH MATERIALIZED VIEW
ALTER RULE COPY CREATE SERVER DROP DOMAIN DROP TEXT SEARCH CONFIGURATION REINDEX
ALTER SCHEMA CREATE ACCESS METHOD CREATE STATISTICS DROP EVENT TRIGGER DROP TEXT SEARCH DICTIONARY RELEASE SAVEPOINT
testdb=#
2. \d
\d+
\d emp
testdb-# \d emp
Table "public.emp"
Column | Type | Collation | Nullable | Default
-----------+-----------------------+-----------+----------+---------
empid | integer | | |
last_name | character varying(20) | | |
testdb-# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | emp | table | postgres
(1 row)
testdb-# \di
Did not find any relations.
testdb-# \ds
Did not find any relations.
testdb-# \dv
Did not find any relations.
testdb-# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+------+------------------+---------------------+------
(0 rows)
3. 显示执行时间
\timing on
testdb=# \timing on
Timing is on.
testdb=# select * from emp;
empid | last_name
-------+-----------
(0 rows)
Time: 0.326 ms
4、列出所有模式
\dn
postgres=# \dn
List of schemas
Name | Owner
--------+-------------------
public | pg_database_owner
(1 row)
postgres=#
5、列出所有表空间
\db
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+----------
pg_default | postgres |
pg_global | postgres |
(2 rows)
6、列出所有用户
\dg \du 都一样
postgres=# \dg
List of roles
Role name | Attributes
-----------+------------------------------------------------------------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
postgres=# \du
List of roles
Role name | Attributes
-----------+------------------------------------------------------------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
postgres=#
7.列出某各表的权限
testdb=# \dp emp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+------+-------+-------------------+-------------------+----------
public | emp | table | | |
(1 row)
8.查看当前字符集
testdb=# \encoding
GBK
9、设置边框
testdb=# \pset border 2
Border style is 2.
testdb=# select * from emp;
+-------+-----------+
| empid | last_name |
+-------+-----------+
| 100 | zq |
+-------+-----------+
(1 row)
Time: 0.338 ms
“\pset”命令设置边框的用法如下。
·\pset border 0:表示输出内容无边框。
·\pset border 1:表示输出内容只有内边框。
·\pset border 2:表示输出内容内外都有边框。
psql中默认的输出格式是“\pset border 1”。
10、分隔符
\pset format unaligned
testdb=# \pset format unaligned
Output format is unaligned.
testdb=#
testdb=#
testdb=# select * from emp;
empid|last_name
100|zq
(1 row)
Time: 0.221 ms
--空格
testdb=# \pset fieldsep '\t'
Field separator is " ".
testdb=#
testdb=#
testdb=# select * from emp;
empid last_name
100 zq
(1 row)
--逗号
testdb=# \pset fieldsep '\,'
Field separator is ",".
testdb=# select * from emp;
empid,last_name
100,zq
101,mahan
(2 rows)
输出到某个位置
testdb=# \o emp.txt
testdb=# select * from emp;
Time: 1.837 ms
testdb=# host
---t 指显示表
不显示表头
testdb=# \t
Tuples only is on.
testdb=# select * from emp;
100 | zq
101 | mahan
testdb=#
11、表行显示按列显示
\x
与 mysql 的\G相似
testdb=# \x
Expanded display is on.
testdb=#
testdb=# select * from emp;
empid | 100
last_name | zq
----------+------
empid | 101
last_name | mahan
testdb=#
12.执行外部文件
testdb=# \i getrunsql
empid | last_name
-------+-----------
100 | zq
101 | mahan
(2 rows)
testdb=# pset
testdb-# pset -t
testdb-# \i getrunsql
empid | last_name
-------+-----------
100 | zq
101 | mahan
(2 rows)
testdb-# \t
Tuples only is on.
testdb-#
testdb-#
testdb-# \i getrunsql
100 | zq
101 | mahan
testdb-#
testdb-#
[postgres@dbserver1 ~]$ psql -x -f getrunsql testdb
-[ RECORD 1 ]----
empid | 100
last_name | zq
-[ RECORD 2 ]----
empid | 101
last_name | mahan
13、关闭自动提交
\set autocommit off
14. 记录命令
|