数据库使用说明

最近更新时间: 2024-06-12 15:06:00

psql是一个基于终端的 TDSQL PostgreSQL 版客户端工具,类似Oracle中的命令行工具sqlplus,但比sqlplus强大多了。它让你能交互式地键入查询,把它们发送给 TDSQL PostgreSQL 版,并且查看查询结果。或者,输入可以来自于一个文件或者命令行参数。此外,psql还提供一些元命令和多种类似 shell 的特性来为编写脚本和自动化多种任务提供便利。psql还支持命令补全,历史命令回找。

连接数据库参数介绍

-d dbname

--dbname=dbname

指定要连接到的数据库名,不指定该参数时默认是从PGDATABASE环境变量中取得(如果被设置)。

-h host

--host=host

指定要连接服务器机器的主机名或者ip地址。不指定该参数时默认是从PGHOST环境变量中取得(如果被设置)。

-p port

--port=port

指定要连接服务器监听的 TCP端口,不指定该参数时默认是从PGPORT环境变量中取得(如果被设置),否则使用编译在程序中的默认值。

-U username

--username=username

指定要连接服务的用户名,不指定该参数时默认是从PGUSER环境变量中取得(如果被设置),否则使用当前操作系统用户名做为默认值。

-w

--no-password

从不发出一个口令提示。如果服务器要求口令认证并且没有其他方式提供口令(例如一个.pgpass文件),那么连接尝试将失败。这个选项对于批处理任务和脚本有用,因为在其中没有一个用户来输入口令。

-W

--password

强制pg_dump在连接到一个数据库之前提示要求一个口令。

连接到一个数据库

  • 使用参数连接

    [pgxz@VM_0_3_centos root]$ psql -h 172.16.0.29 -p 15432 -U tbase -d
    postgres
    
    psql (PostgreSQL 10 (TBase 2.01))
    
    Type "help" for help.
    
    postgres=#
  • 使用conninfo字符串或者一个URI

    [pgxz@VM_0_3_centos root]$ psql
    postgresql://pgxz@172.16.0.29:15432/postgres
    
    psql (PostgreSQL 10 (TBase 2.01))
    
    Type "help" for help.
    
    postgres=#
    
  • 配置证书连接

    [pgxz@VM_0_3_centos ~]$ psql 'host=172.16.0.29 port=15432 dbname=postgres
    user=tbase sslmode=verify-ca sslcert=postgresql.crt sslkey=postgresql.key
    sslroo腾讯云金融专区rt=root.crt'
    
    Password:
    
    psql (PostgreSQL 10 (TBase 2.01))
    
    SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
    
    Type "help" for help.
    
    postgres=> \q
    
  • 配置环境变量后快捷连接

    [pgxz@VM_0_3_centos root]$ PGUSER=tbase
    
    [pgxz@VM_0_3_centos root]$ PGHOST=127.0.0.1
    
    [pgxz@VM_0_3_centos root]$ PGDATABASE=postgres
    
    [pgxz@VM_0_3_centos root]$ PGPORT=15432
    
    [pgxz@VM_0_3_centos root]$ export PGHOST PGUSER PGDATABASE PGPORT
    
    [pgxz@VM_0_3_centos root]$ psql
    
    psql (PostgreSQL 10 (TBase 2.01))
    
    Type "help" for help.
    
    postgres=#
    

    变些环境变量可以配置在

[pgxz@VM_0_3_centos root]$ cat ~/.bashrc

# .bashrc

PGUSER=tbase

PGHOST=127.0.0.1

PGDATABASE=postgres

PGPORT=15432

export PGHOST PGUSER PGDATABASE PGPORT

建立一个新连接

  • 连接到另外一个库(也可以是当前库)

    postgres=# select pg_backend_pid();
    
    pg_backend_pid
    
    ----------------
    
    2408
    
    (1 row)
    
    postgres=# \c
    
    You are now connected to database "postgres" as user "pgxz".
    
    postgres=# select pg_backend_pid();
    
    pg_backend_pid
    
    ----------------
    
    2426
    
    (1 row)
    
    postgres=# \c template1
    
    You are now connected to database "template1" as user "pgxz".
    
    template1=#
    
  • 连接到外一台服务

    postgres=# \c postgres pgxz 172.16.0.47 15432
    
    You are now connected to database "postgres" as user "pgxz" on host
    "172.16.0.47" at port "15432".
    

显示和设置该连接当前运行参数

  • 显示当前连接的运行参数

    postgres=# SELECT CURRENT_USER;
    
    current_user
    
    --------------
    
    pgxz
    
    (1 row)
    
    postgres=# show search_path ;
    
    search_path
    
    ----------------
    
    "$user",public
    
    (1 row)
    
    postgres=# show work_mem ;
    
    work_mem
    
    ----------
    
    4MB
    
    (1 row)
    
  • 设置当前连接的运行参数

    
    postgres=# set search_path = "$user",public,pg_catalog;
    
    SET
    
    postgres=# set work_mem = '8MB';
    
    SET
    
  • 打开和关闭显示每个sql语句执行的时间

    
    postgres=# \timing on
    
    Timing is on.
    
    postgres=# select count(1) from tbase;
    
    count
    
    -------
    
    10000
    
    (1 row)
    
    Time: 5.139 ms
    
    postgres=# \timing off
    
    Timing is off.
    
    postgres=# select count(1) from tbase;
    
    count
    
    -------
    
    10000
    
    (1 row)
    
  • 打开和关闭显示每个快捷操作符实际运行的sql语句

    postgres=# \set ECHO_HIDDEN on
    
    postgres=# \dt
    
    ********* 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' END as "Type",
    
    pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
    
    FROM pg_catalog.pg_class c
    
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
    
    WHERE c.relkind IN ('r','')
    
    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;
    
    **************************
    
    List of relations
    
    Schema | Name | Type | Owner
    
    --------+--------------+-------+-------
    
    public | t_time_range | table | pgxz
    
    public | tbase | table | pgxz
    
    (2 rows)
    
    postgres=# \set ECHO_HIDDEN off
    
    postgres=# \dt
    
    List of relations
    
    Schema | Name | Type | Owner
    
    --------+--------------+-------+-------
    
    public | t_time_range | table | pgxz
    
    public | tbase | table | pgxz
    
    (2 rows)
    
  • 配置输出结果为HTML格式

    postgres=# \pset format html
    
    Output format is html.
    
    postgres=# \d tbase
    
    <table border="1">
    
    <caption>Table \&quot;public.tbase&quot;</caption>
    
    <tr>
    
    <th align="center">Column</th>
    
    <th align="center">Type</th>
    
    <th align="center">Modifiers</th>
    
    </tr>
    
    <tr valign="top">
    
    <td align="left">id</td>
    
    <td align="left">integer</td>
    
    <td align="left">\&nbsp; </td>
    
    </tr>
    
    <tr valign="top">
    
    <td align="left">mc</td>
    
    <td align="left">text</td>
    
    <td align="left">\&nbsp; </td>
    
    </tr>
    
    </table>
    
  • 恢复为对齐模式

    postgres=# \pset format aligned
    
    Output format is aligned.
    
    postgres=# \d tbase
    
    Table "public.tbase"
    
    Column | Type | Modifiers
    
    --------+---------+-----------
    
    id | integer |
    
    mc | text |
    
  • 配置行列显示格式

    postgres=# \x on
    
    Expanded display is on.
    
    postgres=# select * from tbase where id=1;
    
    -[ RECORD 1 ]
    
    id | 1
    
    mc | 1
    
    -[ RECORD 2 ]
    
    id | 1
    
    mc | 2
    
    -[ RECORD 3 ]
    
    id | 1
    
    mc | 2
    
    postgres=# \x off
    
    Expanded display is off.
    
    postgres=# select * from tbase where id=1;
    
    id | mc
    
    ----+----
    
    1 | 1
    
    1 | 2
    
    1 | 2
    
    (3 rows)
    
  • 显示和配置客户端编码

    postgres=# \encoding
    
    UTF8
    

    配置客户端编码为SQL_ASCII

postgres=# \encoding sql_ascii

postgres=# \encoding

SQL_ASCII

退出连接

postgres=# \q

psql执行一个sql命令

  • 显示标题

    [pgxz@VM_0_3_centos root]$ psql -h 172.16.0.29 -p 15432 -U tbase -d
    postgres -c "select count(1) from pg_class"
    
    count
    
    -------
    
    317
    
    (1 row)
    
  • 不显示标题

    [pgxz@VM_0_3_centos root]$ psql -h 172.16.0.29 -p 15432 -U tbase -d
    postgres -t -c "select count(1) from pg_class"
    
    317