- [maxwell@MaxwellDBA ~]$ psql -d maxwelldb
- psql (12.9)
- Type "help" for help.
-
- maxwelldb=# select version();
- version
- ------------------------------------------------------------------------------------------------------------
- PostgreSQL 12.9 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4), 64-bit
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select pg_postmaster_start_time();
- pg_postmaster_start_time
- -------------------------------
- 2022-08-30 13:09:57.389012+08
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select pg_conf_load_time();
- pg_conf_load_time
- -------------------------------
- 2022-08-30 13:09:57.371297+08
- (1 row)
-
- maxwelldb=#
- osdba@db01:~$ pg_ctl reload
- server signaled osdba@db01:~$ psql
- psql (9.3.2) Type "help" for help.
- osdba=# select pg_conf_load_time();
- pg_conf_load_time
- -------------------------------
- 2014-07-19 09:36:06.292696+00
- (1 row)
- maxwelldb=# show timezone;
- TimeZone
- ---------------
- Asia/Shanghai
- (1 row)
-
- maxwelldb=#
注意,数据库的时区有时并不是当前操作系统的时区,此时在数据库中看到的时间就与在操作系统中看到的不一致,示例如下:
- maxwelldb=# select now();
- now
- -------------------------------
- 2022-08-30 14:58:29.949507+08
- (1 row)
-
- maxwelldb=#
- [maxwell@MaxwellDBA ~]$ psql -l
- List of databases
- Name | Owner | Encoding | Collate | Ctype | Access privileges
- -----------+----------+----------+-------------+-------------+-----------------------
- maxwell | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- maxwelldb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- 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
- (5 rows)
-
- [maxwell@MaxwellDBA ~]$ psql -d maxwelldb
- psql (12.9)
- Type "help" for help.
-
- maxwelldb=# \
- invalid command \
- Try \? for help.
- maxwelldb=#
- maxwelldb=# \l
- List of databases
- Name | Owner | Encoding | Collate | Ctype | Access privileges
- -----------+----------+----------+-------------+-------------+-----------------------
- maxwell | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- maxwelldb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
- 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
- (5 rows)
-
- maxwelldb=#
- maxwelldb=# select user;
- user
- ---------
- maxwell
- (1 row)
-
- maxwelldb=# select current_user;
- current_user
- --------------
- maxwell
- (1 row)
-
- maxwelldb=#
上例中使用current_user与使用user的结果是完全相同的。
- maxwelldb=# select session_user;
- session_user
- --------------
- maxwell
- (1 row)
-
- maxwelldb=#
注意,通常情况下“session_user”与“user”是相同的。但当用命令“SET ROLE”改变用户的角色时,这两者就不同了,示例如下:
- maxwelldb=# set role postgres;
- SET
- maxwelldb=# select session_user;
- session_user
- --------------
- maxwell
- (1 row)
-
- maxwelldb=# select user;
- user
- ----------
- postgres
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select current_catalog,current_database();
- current_catalog | current_database
- -----------------+------------------
- maxwelldb | maxwelldb
- (1 row)
-
- maxwelldb=#
注意,使用current_catalog与current_database()都显示当前连接的数据库名称,两者的功能完全相同,只不过catalog是SQL标准中的用语。
- maxwelldb=# select inet_client_addr(),inet_client_port();
- inet_client_addr | inet_client_port
- ------------------+------------------
- |
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select inet_server_addr(),inet_server_port();
- inet_server_addr | inet_server_port
- ------------------+------------------
- |
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select pg_backend_pid();
- pg_backend_pid
- ----------------
- 341331
- (1 row)
-
- maxwelldb=#
- [maxwell@MaxwellDBA ~]$ ps -ef|grep 340540 | grep -v grep
- postgres 340540 1 0 13:09 ? 00:00:00 /usr/bin/postmaster -D /var/lib/pgsql/data
- postgres 340541 340540 0 13:09 ? 00:00:00 postgres: logger
- postgres 340543 340540 0 13:09 ? 00:00:00 postgres: checkpointer
- postgres 340544 340540 0 13:09 ? 00:00:00 postgres: background writer
- postgres 340545 340540 0 13:09 ? 00:00:00 postgres: walwriter
- postgres 340546 340540 0 13:09 ? 00:00:00 postgres: autovacuum launcher
- postgres 340547 340540 0 13:09 ? 00:00:00 postgres: stats collector
- postgres 340548 340540 0 13:09 ? 00:00:00 postgres: logical replication launcher
- [maxwell@MaxwellDBA ~]$
- [maxwell@MaxwellDBA ~]$ psql -d maxwelldb
- psql (12.9)
- Type "help" for help.
-
- maxwelldb=# show shared_buffers;
- shared_buffers
- ----------------
- 128MB
- (1 row)
-
- maxwelldb=# select current_setting('shared_buffers');
- current_setting
- -----------------
- 128MB
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select set_config('maintenance_work_mem','128MB',false);
- set_config
- ------------
- 128MB
- (1 row)
-
- maxwelldb=# set maintenance_work_mem to '128MB';
- SET
- maxwelldb=# select set_config('maintenance_work_mem','128MB',false);
- set_config
- ------------
- 128MB
- (1 row)
-
- maxwelldb=#


- maxwelldb=# select pg_is_in_backup(),pg_backup_start_time();
- pg_is_in_backup | pg_backup_start_time
- -----------------+----------------------
- f |
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select pg_is_in_recovery();
- pg_is_in_recovery
- -------------------
- f
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select pg_database_size('maxwelldb'),pg_size_pretty(pg_database_size('maxwelldb'));
- pg_database_size | pg_size_pretty
- ------------------+----------------
- 8242031 | 8049 kB
- (1 row)
-
- maxwelldb=#
上面的命令用于查看数据库“maxwelldb”的大小。注意,如果数据库中有很多表,使用上述命令查询将比较慢,也可能对当前系统产生不利的影响。在上面的命令中,pg_size_pretty()函数会把数字以MB、GB等格式显示出来,这样的结果更加直观。
- maxwelldb=# select pg_size_pretty(pg_relation_size('playground'));
- pg_size_pretty
- ----------------
- 8192 bytes
- (1 row)
-
- maxwelldb=# select pg_size_pretty(pg_total_relation_size('playground'));
- pg_size_pretty
- ----------------
- 24 kB
- (1 row)
-
- maxwelldb=#
上例中,pg_relation_size()仅计算表的大小,不包括索引的大小,而pg_total_relation_size()则会把表上索引的大小也计算进来。
- maxwelldb=# select pg_size_pretty(pg_indexes_size('playground'));
- pg_size_pretty
- ----------------
- 16 kB
- (1 row)
-
- maxwelldb=#
注意,pg_indexes_size()函数的参数名是一个表对应的OID(输入表名会自动转换成表的OID),而不是索引的名称。
- maxwelldb=# select pg_size_pretty(pg_tablespace_size('pg_global'));
- pg_size_pretty
- ----------------
- 623 kB
- (1 row)
-
- maxwelldb=#
- maxwelldb=# select pg_size_pretty(pg_tablespace_size('pg_default'));
- pg_size_pretty
- ----------------
- 39 MB
- (1 row)
-
- maxwelldb=#
上面的示例中查看了全局表空间“pg_global”和默认表空间“pg_default”的大小。
- maxwelldb=# select pg_relation_filepath('playground');
- pg_relation_filepath
- ----------------------
- base/16385/16389
- (1 row)
-
- maxwelldb=#