--- View all help
postgres=# \?
--- View SQL help
postgres=# \h
---Switch database
postgres=# \c database_name
---Switch user
postgres=# \c - username
---Switch database and user at the same time
postgres=#\c database_name username
--- View connection information
postgres=# \conninfo
---Display borders inside and outside the output information
postgres=#\pset border 2
--- View table information
postgres=# \d
---View index information
postgres=#\di
--- View view information
postgres=# \dv
--- View sequence information
postgres=# \ds
---View database information
postgres=# \l
--- View historical commands
postgres=# \s
--- View table space information
postgres=# \db
---View schema information
postgres=# \dn
--- View function information
postgres=# \df
--- View permission information
postgres=# \dp
or
postgres=# \z
--- View users or roles
postgres=# \you
or
postgres=# \dg
--- View installed extensions
postgres=# \dx
--- View external table
postgres=# \it
---View user mapping
postgres=# \gave
---View partition
postgres=# \dP
---View partition index
postgres=# \dPi
---View all triggers under the current user under the current database
postgres=# select * from pg_trigger;
--- Calculate object size
postgres=# select pg_size_pretty(pg_relation_size('xx'));
---The query display is switched to vertical display
postgres=# \x
--- View process port
netstat -anltp|grep postgres
---View postgres process
ps -ef|grep ^postgres
---View postgres process ptree format
ps -ajxf|grep postgres
---Check the automatic commit status of the current library, the default is on, open automatic commit
postgres=# \echo :AUTOCOMMIT
Turn off the automatic commit of the current library
postgres=#\set AUTOCOMMIT off
Open the current library to automatically commit
postgres=# \set AUTOCOMMIT on
---View the table space number and data file number of the table
postgres==# select reltablespace,relfilenode from pg_class where relname='jobs'; reltablespace | relfilenode ---------------+------------- 0 | 16408 (1 row)
0 means the database default table space, 16408 means the data file number of the table
---Find configuration file location
postgres=# show config_file;
--- Reload configuration file
postgres=# select pg_reload_conf();
--- Determine whether to restart the service after parameter modification
postgres=# select name,setting,pending_restart from pg_settings where name='autovacuum';
name | setting | pending_restart
------------+---------+-----------------
autovacuum | on | f
(1 row)
f means no restart is required.
--- Statistics sql running time:
\timing on -- open
\timing off -- off
or execute directly
\timing on or off
---Repeatedly execute the sql statement
test1=> select count(*) from company;\watch 2 --- Repeatedly execute sql, execute once every 2 seconds, you can use ctrl+c to stop
---The SQL statement actually executed by a command is displayed in the running psql
\set ECHO_HIDDEN on --- Turn on the display
\set ECHO_HIDDEN off --- turn off the display
---View the timestamp when the current transaction starts (the results of the three methods are the same)
- mydb=# select now();
- mydb=# select current_timestamp;
- mydb=# select transaction_timestamp();
transaction_timestamp
-------------------------------
2022-08-02 17:20:39.969107+08
-----View installed extension functions---
postgres=# \dx
List of installed extensions
Name | Version | Schema | Description
---------------+---------+------------+----------------------------------------------------------------
pg_visibility | 1.2 | public | examine the visibility map (VM) and page-level visibility info
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
postgres_fdw | 1.1 | public | foreign-data wrapper for remote PostgreSQL servers
(3 rows)
---- View the specific function modules provided by the function
postgres=# \dx+ postgres_fdw;
Objects in extension "postgres_fdw"
Object description
---------------------------------------------
foreign-data wrapper postgres_fdw
function postgres_fdw_disconnect_all()
function postgres_fdw_disconnect(text)
function postgres_fdw_get_connections()
function postgres_fdw_handler()
function postgres_fdw_validator(text[],oid)
(6 rows)
postgres=# \dx+ pg_visibility
Objects in extension "pg_visibility"
Object description
-----------------------------------------------
function pg_check_frozen(regclass)
function pg_check_visible(regclass)
function pg_truncate_visibility_map(regclass)
function pg_visibility_map(regclass)
function pg_visibility_map(regclass,bigint)
function pg_visibility_map_summary(regclass)
function pg_visibility(regclass)
function pg_visibility(regclass,bigint)
(8 rows)
----Grant ordinary users permission to use extension package functions
grant execute on function pg_visibility_map to XX;
---- View archive status
select * from pg_stat_archiver;
----View database and default tablespace
$oid2name
All databases: Oid Database Name Tablespace ---------------------------------- 17138 gjjdb pg_default 17174 jianyedb tbs1 17707 mydb pg_default 17139 nongkendb pg_default 13892 postgres pg_default 13891 template0 pg_default 1 template1 pg_default 17294 test1 pg_default
------- View all namespaces (schema) in the cluster, including system schema
postgres=# select * from pg_catalog.pg_namespace ;
Among them, information_schema is provided to facilitate users to view table/view/function information. Most of them are views. MySQL and SQL Server also have the information_schema schema.
pg_catalog is the system schema, including the system's own functions/data type definitions, etc. pg_catalog is an important cornerstone to ensure the normal operation of postgres.
--------View all schemas of the cluster
postgres=# \dn
--- View the collection of databases that have objects in the specified tablespace
test1=# select * from pg_tablespace; test1=# select pg_tablespace_databases(1663); test1=# select oid,datname from pg_database;
-----Toggle new syslog
test1=# select pg_rotate_logfile();
------Check the size of a certain database
select pg_database_size('database name');
------ View multiple database sizes
select pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size from pg_database;
------- View the table index size under the public schema in order
select indexrelname, pg_size_pretty(pg_relation_size(relid))
from pg_stat_user_indexes where schemaname='public'
order by pg_relation_size(relid) desc;
------- View the size of all tables in the public schema
select relname, pg_size_pretty(pg_relation_size(relid))
from pg_stat_user_tables
where schemaname='public'
order by pg_relation_size(relid) desc;
------Check the size of the specified table under the public schema
select relname, pg_size_pretty(pg_relation_size(relid))
from pg_stat_user_tables
where schemaname='public' and relname='表名'
order by pg_relation_size(relid) desc;
---- View the current log file name
postgres=# select pg_walfile_name(pg_current_wal_lsn());
pg_walfile_name
--------------------------
00000001000000000000002B
(1 row)
---- View the current log file name and log offset
postgres=# SELECT * FROM pg_walfile_name_offset(pg_current_wal_lsn());
file_name | file_offset
--------------------------+-------------
00000001000000000000002B | 1672
(1 row)
--Recycle public default permissions
1) REVOKE CREATE ON SCHEMA public FROM PUBLIC #The first public represents the schema, and the second public represents all users, that is, the operating authority of the public schema is recovered from all users
2) ALTER ROLE ALL SET search_path = "$user" #Delete the public schema from the default search_path search path