[05:17:36-postgres@centos1:/var/lib/pgsql]$ psql -E -c '\l' ********* QUERY ********** SELECT d.datname as "Name", pg_catalog.pg_get_userbyid(d.datdba) as "Owner", pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding", d.datcollate as "Collate", d.datctype as "Ctype", pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges" FROM pg_catalog.pg_database d ORDER BY 1; **************************
List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+---------+-------+----------------------- mydb | postgres | UTF8 | C | C | =Tc/postgres + | | | | | postgres=CTc/postgres+ | | | | | mydb=CTc/postgres postgres | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres (4 rows)
技巧0005(-f):
使用-f filename或者–file=filename,执行一个SQL文件 (始自PG7.1)
1 2 3 4 5 6 7 8
[05:21:03-postgres@centos1:/var/lib/pgsql]$ cat >> tmp.sql << EOF > select * from test; > EOF [05:23:35-postgres@centos1:/var/lib/pgsql]$ psql -f tmp.sql id | col2 ----+------ 1 | test (1 row)
技巧0006(-f和-c):
组合使用-f和-c两个选项 (始自PG7.1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[05:25:15-postgres@centos1:/var/lib/pgsql]$ psql -f tmp.sql -c 'select tableoid, * from test' -c 'select count(*) from test' id | col2 ----+------ 1 | test (1 row)
tableoid | id | col2 ----------+----+------ 16392 | 1 | test (1 row)
[05:44:17-postgres@centos1:/var/lib/pgsql]$ psql -d "host=localhost port=5555 dbname=mydb" Password for user postgres: psql (14.7) Type "help" for help.
mydb=# \q [05:44:29-postgres@centos1:/var/lib/pgsql]$ psql "host=localhost port=5555 dbname=mydb" Password for user postgres: psql (14.7) Type "help" for help.
[06:00:12-postgres@centos1:/var/lib/pgsql]$ psql -h localhost Password for user postgres: psql (14.7) Type "help" for help.
postgres=# \q [06:03:51-postgres@centos1:/var/lib/pgsql]$ psql -h /tmp psql (14.7) Type "help" for help.
postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5555". postgres=# \q [06:04:09-postgres@centos1:/var/lib/pgsql]$ export PGHOST=localhost [06:04:19-postgres@centos1:/var/lib/pgsql]$ psql Password for user postgres: psql (14.7) Type "help" for help.
postgres=# \conninfo You are connected to database "postgres" as user "postgres" on host "localhost" (address "127.0.0.1") at port "5555". postgres=# \q [06:04:36-postgres@centos1:/var/lib/pgsql]$ unset PGHOST
[06:04:46-postgres@centos1:/var/lib/pgsql]$ psql psql (14.7) Type "help" for help.
postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5555".
[06:26:50-postgres@centos1:/var/lib/pgsql]$ psql -c "select * from test" -o tmp.log [06:26:55-postgres@centos1:/var/lib/pgsql]$ cat tmp.log id | col2 ----+------ 1 | test (1 row)
技巧0028 (-p)
-p 或者 –port=port指定连接的端口号, 始自PG7.1
1 2 3
[06:26:58-postgres@centos1:/var/lib/pgsql]$ psql -p 5555 psql (14.7) Type "help" for help.
技巧0029 (-p处理顺序)
-p的处理顺序: 始自PG7.1
-p提供的值
$PGPORT
编译时指定的端口值(通常是5432)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[06:30:21-postgres@centos1:/var/lib/pgsql]$ grep -E "port=" 14/data/postgresql.conf port=5555 [06:30:27-postgres@centos1:/var/lib/pgsql]$ psql -p 5555 psql (14.7) Type "help" for help.
postgres=# \q [06:30:48-postgres@centos1:/var/lib/pgsql]$ export PGPORT=5555 [06:31:23-postgres@centos1:/var/lib/pgsql]$ psql psql (14.7) Type "help" for help.
postgres=# \q [06:31:25-postgres@centos1:/var/lib/pgsql]$ unset PGPORT [06:31:28-postgres@centos1:/var/lib/pgsql]$ psql psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket?
技巧0030 (-q)
-q会让psql更安静, 始自PG7.1
1 2 3 4 5 6 7
[06:32:54-postgres@centos1:/var/lib/pgsql]$ psql psql (14.7) Type "help" for help.
[06:34:10-postgres@centos1:/var/lib/pgsql]$ cat tmp.sql select * from test;
select 1 as foo; [06:35:47-postgres@centos1:/var/lib/pgsql]$ psql -s -f tmp.sql ***(Single step mode: verify command)******************************************* select * from test; ***(press return to proceed or enter x and return to cancel)********************
id | col2 ----+------ 1 | test (1 row)
***(Single step mode: verify command)******************************************* select 1 as foo; ***(press return to proceed or enter x and return to cancel)********************
foo ----- 1 (1 row)
技巧0032 (-t)
使用-t 或者 –tuples-only,将关闭输出列名,只显示结果和行数。始自PG7.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[05:54:52-postgres@sean-rh1:/var/lib/pgsql]$ psql -c 'select * from t' id ----- 100 300 200 100 (4 rows)
[06:00:56-postgres@sean-rh1:/var/lib/pgsql]$ psql -U postgres psql (14.7) Type "help" for help.
postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5555". postgres=# \q [06:01:31-postgres@sean-rh1:/var/lib/pgsql]$ export PGUSER=postgres [06:01:40-postgres@sean-rh1:/var/lib/pgsql]$ psql psql (14.7) Type "help" for help. postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5555". postgres=# \q [06:01:45-postgres@sean-rh1:/var/lib/pgsql]$ unset PGUSER [06:02:07-postgres@sean-rh1:/var/lib/pgsql]$ psql psql (14.7) Type "help" for help.
postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5555".
技巧0036 (长选项)
长选项可以用等号,也可以不用等号。(PG9.1)
1 2 3 4 5 6 7 8 9 10
[06:03:39-postgres@sean-rh1:/var/lib/pgsql]$ psql --dbname mydb psql (14.7) Type "help" for help.
mydb=# \q [06:03:46-postgres@sean-rh1:/var/lib/pgsql]$ psql --dbname=mydb psql (14.7) Type "help" for help.
# "local" is for Unix domain socket connections only local postgres postgres scram-sha-256 local all all trust
[06:31:39-postgres@sean-rh1:/var/lib/pgsql/14/data]$ psql -U postgres -w psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5555" failed: fe_sendauth: no password supplied
技巧0041 (-W)
强行输入 密码,即算不需要密码也要输入 。始自PG7.1
1 2 3 4
[06:31:48-postgres@sean-rh1:/var/lib/pgsql/14/data]$ psql -W Password: psql (14.7) Type "help" for help.
[08:31:24-postgres@sean-rh1:/var/lib/pgsql]$ cat > query.sql << EOF > INSERT INTO t values(1); > INSERT INTO t values('test'); > select count(*) from t; > EOF
[08:32:19-postgres@sean-rh1:/var/lib/pgsql]$ psql -1 -f query.sql INSERT 0 1 psql:query.sql:2: ERROR: invalid input syntax for type integer: "test" LINE 1: INSERT INTO t values('test'); ^ psql:query.sql:3: ERROR: current transaction is aborted, commands ignored until end of transaction block
postgres=# \set PROMPT1 demo1:/> demo1:/>\set PROMPT2 demo2_wating demo1:/>select * demo2_wating from t; id ----- 100 300 200 100 (4 rows)
demo1:/>\set PROMPT3 'please enter your value: ' demo1:/>copy t(id) from stdin demo2_wating; Enter data to be copied followed by a newline. End with a backslash and a period on a line by itself, or an EOF signal. please enter your value: 350 please enter your value: 480 please enter your value: \. COPY 2
mydb=> \c postgres postgres You are now connected to database "postgres" as user "postgres". postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- U1 | | {} demo | | {} mydb | | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} u1 | | {}
postgres=# \c mydb mydb You are now connected to database "mydb" as user "mydb". mydb=>
技巧0050 (PROMPT)
可以在PROMPT里头设置%M,用以表示完整的server host。始自PG7.1
1 2 3 4 5 6
[10:51:53-postgres@sean-rh1.openstack.eu-nl-1.cloud.sap:/var/lib/pgsql]$ psql -h sean-rh1.openstack.eu-nl-1.cloud.sap psql (14.7) Type "help" for help.
[10:51:53-postgres@sean-rh1.openstack.eu-nl-1.cloud.sap:/var/lib/pgsql]$ psql -h sean-rh1.openstack.eu-nl-1.cloud.sap psql (14.7) Type "help" for help.
postgres@postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5555". postgres@postgres=# \c mydb You are now connected to database "mydb" as user "postgres". postgres@mydb=# \c You are now connected to database "mydb" as user "postgres". postgres@mydb=# \conninfo You are connected to database "mydb" as user "postgres" via socket in "/var/run/postgresql" at port "5555".
技巧0059 (元命令)
\开始的命令都被认为是元命令,由PSQL处理。始自PG7.1
1 2 3
postgres@mydb=# \ttt invalid command \ttt Try \? for help.
技巧0060 (\c)
交互模式下,您可以用\c去连接数据库。
1 2 3 4 5 6 7 8 9 10
[11:17:54-postgres@sean-rh1.openstack.eu-nl-1.cloud.sap:/var/lib/pgsql]$ psql psql (14.7) Type "help" for help.
postgres=# \c You are now connected to database "postgres" as user "postgres". postgres=# \c mydb mydb You are now connected to database "mydb" as user "mydb". mydb=> \conninfo You are connected to database "mydb" as user "mydb" via socket in "/var/run/postgresql" at port "5555".
技巧0061 (\c)
\c可以带多个参数,指定更多的信息:\c dbname username host port
1 2
mydb=> \c postgres mydb localhost 5555 You are now connected to database "postgres" as user "mydb" on host "localhost" (address "::1") at port "5555".
psql:query.sql:2: error: \connect: connection to server on socket "/var/run/postgresql/.s.PGSQL.5555" failed: FATAL: database "aaaa" does not exist
技巧0063 (\c)
你可以使用’-‘来省略一些相同的参数信息, 始自PG9.2
1 2 3 4
postgres=# \c - mydb - 5555 You are now connected to database "postgres" as user "mydb". postgres=> \conninfo You are connected to database "postgres" as user "mydb" via socket in "/var/run/postgresql" at port "5555".
技巧0064 (\c)
可以使用连接串来进行连接, 始自PG9.4
1 2 3 4
postgres=> \conninfo You are connected to database "postgres" as user "mydb" via socket in "/var/run/postgresql" at port "5555". postgres=> \c postgresql://localhost:5555 You are now connected to database "postgres" as user "postgres" on host "localhost" (address "::1") at port "5555".
技巧0065 (\c)
\c元命令,也可以用健值串来创建连接, 始自PG8.3
1 2 3 4
postgres=# \c "dbname=mydb user=mydb host=localhost port=5555" You are now connected to database "mydb" as user "mydb". mydb=> \conninfo You are connected to database "mydb" as user "mydb" on host "localhost" (address "::1") at port "5555".
技巧0066 (\C)
\C可以用于指定查询的标题。撤掉设置,直接\C 始自PG7.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
postgres=# \C 'test result' Title is "test result". postgres=# select * from t limit 1; test result id ----- 100 (1 row)
postgres=# \C Title is unset. postgres=# select * from t limit 1; id ----- 100 (1 row)
技巧0067 (\conninfo)
\conninfo用于显示 完整的连接信息. 始自PG9.5
1 2
postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5555".
postgres=# \copyright PostgreSQL Database Management System (formerly known as Postgres, then as Postgres95)
Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
Portions Copyright (c) 1994, The Regents of the University of California
Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.