5.psql客户端使用

1.登录

本地操作系统认证机制

1.1 本地操作系统认证机制
[postgres@pg11-1 ~]$ psql
psql (11.2)
Type "help" for help.
postgres=#

1.2 tcp ip登录
[postgres@pg11-1 ~]$ psql -h192.162.152.11 -p5432 -Upostgres -dpostgres
Password for user postgres:
psql (11.2)
Type "help" for help.
postgres=#

2.psql调用命令

2.1 直接调用命令
[postgres@pg11-1 ~]$ psql -c "select 'hello word';"
  ?column?
------------
 hello word
(1 row)

2.2 调用sql文件
[postgres@pg11-1 ~]$ echo "select 'hello word';" >  hello_world.sql
[postgres@pg11-1 ~]$ psql -f hello_world.sql
  ?column?
------------
 hello word
(1 row)

3.psql元命令

3.1 数据库对象信息查询
\d
\dxx[S+]
-- xx各种不同对象
-- S显示系统对象
-- +显示详细信息

3.2 查看/切换数据库
\l
testdb=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 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
 testdb    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

testdb=# \c postgres
You are now connected to database "postgres" as user "postgres".

3.3 查看所有表信息
testdb=# \d
        List of relations
 Schema | Name | Type  |  Owner
--------+------+-------+----------
 public | t1   | table | postgres
 public | t2   | table | postgres
(2 rows)

testdb=# \d+
                      List of relations
 Schema | Name | Type  |  Owner   |    Size    | Description
--------+------+-------+----------+------------+-------------
 public | t1   | table | postgres | 8192 bytes |
 public | t2   | table | postgres | 1448 kB    |
(2 rows)

3.4 查看单表信息
testdb=# \d t3
                        Table "public.t3"
 Column |         Type          | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
 id     | integer               |           | not null |
 name   | character varying(16) |           |          |
Indexes:
    "pk_t3" PRIMARY KEY, btree (id)
    "idx_t3_name" btree (name)

testdb=# \d+ t3
                                            Table "public.t3"
 Column |         Type          | Collation | Nullable | Default | Storage  | Stats target | Description
--------+-----------------------+-----------+----------+---------+----------+--------------+-------------
 id     | integer               |           | not null |         | plain    |              |
 name   | character varying(16) |           |          |         | extended |              |
Indexes:
    "pk_t3" PRIMARY KEY, btree (id)
    "idx_t3_name" btree (name)

3.5 模糊匹配  * 或者 ?
\d t*
\d t?
                        Table "public.t1"
 Column |         Type          | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
 id     | integer               |           |          |
 name   | character varying(16) |           |          |

                        Table "public.t2"
 Column |         Type          | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
 id     | integer               |           |          |
 name   | character varying(16) |           |          |

3.6 查看表空间信息
\db
\db+
                                             List of tablespaces
    Name    |  Owner   |            Location            | Access privileges | Options |  Size   | Description
------------+----------+--------------------------------+-------------------+---------+---------+-------------
 pg_default | postgres |                                |                   |         | 31 MB   |
 pg_global  | postgres |                                |                   |         | 574 kB  |
 tbs_test   | postgres | /u01/pgsql11/tbs_test          |                   |         | 0 bytes |

3.7 其他元命令
\dt:只显示表
\di:显示索引
\dv:显示视图
\df:显示函数
\dn:显示schema
\du或\dg:显示用户或角色

4.显示psql元命令的实际sql

psql -E 或 \set ECHO_HIDDEN on

[postgres@pg11-1 ~]$ psql -E
psql (11.2)
Type "help" for help.

testdb=# \d+
********* QUERY **********
SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'table' WHEN 'I' THEN 'index' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
  pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as "Size",
  pg_catalog.obj_description(c.oid, 'pg_class') as "Description"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','v','m','S','f','')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
      AND n.nspname !~ '^pg_toast'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;

5.psql常用设置

\timing on :显示执行时间
\o :接下来的sql执行结果将写到指定文件
\i :执行外部sql文件(类似于psql -f)
\x :以列式显示每行数据
\pset :输出显示格式
  \pset  border 0:输出无边框
  \pset  border 1:输出有内边框
  \pset  border 2:输出有内外边框

testdb=# \o /home/postgres/1.log
testdb=# select * from t1 limit 1;
testdb=# \q
[postgres@pg11-1 ~]$ cat /home/postgres/1.log
 id | name
----+-------
  1 | AAAAA
(1 row)

6.psql关闭自动提交

testdb=# \set AUTOCOMMIT off
testdb=# delete from t1;
DELETE 4
testdb=# rollback;
ROLLBACK

7.psql客户端字符集设置

查看数据库编码
testdb=# \encoding
UTF8
查看客户端编码
testdb=# show client_encoding;
 client_encoding
-----------------
 UTF8
修改客户端编码
testdb=# set client_encoding to 'GBK';
SET
testdb=# show client_encoding;
 client_encoding
-----------------
 GBK

8.自定义psql缺省环境

vi ~/.psqlrc
\timing on
\set AUTOCOMMIT off

你可能感兴趣的:(5.psql客户端使用)