美文网首页
5.psql客户端使用

5.psql客户端使用

作者: 善良的良 | 来源:发表于2019-05-18 22:40 被阅读0次

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客户端使用

    1.登录 本地操作系统认证机制 2.psql调用命令 3.psql元命令 4.显示psql元命令的实际sql ps...

  • Curator 使用(一)

    Curator 使用 (一) POM 依赖 客户端初始化 客户端代码初始化如下所示: 重试策略 客户端是使用建造者...

  • socket.io

    客户端使用 引入socket.io客户端的js 服务器使用 socket.io使用方式总结 一 on 和emit事...

  • ZooKeeper之data tree

    应用使用ZookKeeper客户端库使用ZooKeeper服务。ZK客户端负责于ZK集群交互。 ZooKeeper...

  • zookeeper常用命令

    1. zookeeper客户端的使用 1.1 使用客户端连接zookeeper 步骤一:进入zookeeper的b...

  • 客户端技术笔记

    1.常用客户端技术介绍 客户端是指普通用户使用的终端,用户通过客户端接触并使用产品。客户端通常是指PC与移动端。 ...

  • clickhouse入门教程,基础命令

    客户端基本使用 clickhouse 客户端通过clickhouse-client执行 1.1 登录客户端 hos...

  • 如何在 mac 上使用 telegram 客户端

    telegram 客户端,发现翻墙后,使用依然无法使用。 其实只需要配置下客户端的代理,直接

  • Socket的使用回顾

    使用socket连接 使用socket 服务端 使用socket客户端

  • IdentityServer4[3]:使用客户端认证控制API访

    使用客户端认证控制API访问(客户端授权模式) 场景描述 使用IdentityServer保护API的最基本场景。...

网友评论

      本文标题:5.psql客户端使用

      本文链接:https://www.haomeiwen.com/subject/vnvmaqtx.html