查看postgresql系統(tǒng)信息的常用命令操作
1、查看當(dāng)前數(shù)據(jù)庫(kù)實(shí)例版本。
postgres=# select version(); version ----------------------------------------------------------------------------------------------------------- PostgreSQL 9.3.0 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), 64-bit (1 row)
2、查看數(shù)據(jù)庫(kù)啟動(dòng)時(shí)間。
postgres=# select pg_postmaster_start_time(); pg_postmaster_start_time ------------------------------- 2019-08-26 10:53:47.328699+08 (1 row)
3、查看最后load配置文件的時(shí)間,可以使用pg_ctl reload改變配置的裝載時(shí)間。
postgres=# select pg_conf_load_time(); pg_conf_load_time ------------------------------ 2019-08-26 10:53:46.57045+08 (1 row)
4、顯示當(dāng)前數(shù)據(jù)庫(kù)時(shí)區(qū)。
postgres=# show timezone; TimeZone ---------- PRC (1 row)
5、顯示數(shù)據(jù)庫(kù)的時(shí)間,有時(shí)數(shù)據(jù)庫(kù)的時(shí)區(qū)不是當(dāng)前操作系統(tǒng)的時(shí)區(qū),這時(shí)在數(shù)據(jù)庫(kù)中看到的時(shí)間就與操作系統(tǒng)中看到的時(shí)間不一樣。
postgres=# select now(); now ------------------------------- 2019-08-26 10:58:36.508472+08 (1 row)
6、查看當(dāng)前用戶名,current_user與user是完全相同的。
postgres=# select user; current_user -------------- postgres (1 row) postgres=# select current_user; current_user -------------- postgres (1 row)
7、查看session用戶,通常情況下,session_user與user是相同的。但使用set role改變用戶角色時(shí),session_user始終是那個(gè)原始用戶,而user是當(dāng)前的角色用戶。
postgres=# select session_user; session_user -------------- postgres (1 row) postgres=# set role=aaa; SET postgres=> select session_user; session_user -------------- postgres (1 row) postgres=> select user; current_user -------------- aaa (1 row)
8、查詢當(dāng)前連接的數(shù)據(jù)庫(kù)名稱,使用current_catalog和current_database()都顯示當(dāng)前連接的數(shù)據(jù)庫(kù)名稱,這兩者功能完全相同,只不過catalog是SQL標(biāo)準(zhǔn)中的用語。
postgres=# select current_catalog,current_database(); current_database | current_database ------------------+------------------ postgres | postgres (1 row)
9、查看當(dāng)前session所在客戶端的IP地址及端口(僅限TCP-IP連接,如果是UDP連接的話,查詢結(jié)果IP與port都顯示空)。
postgres=# select inet_client_addr(),inet_client_port(); inet_client_addr | inet_client_port ------------------+------------------ | (1 row)
10、查詢當(dāng)前數(shù)據(jù)庫(kù)服務(wù)器的IP地址及端口(僅限TCP-IP連接,如果是UDP連接的話,查詢結(jié)果IP與port都顯示空)。
postgres=# select inet_server_addr(),inet_server_port(); inet_server_addr | inet_server_port ------------------+------------------ 192.168.91.5 | 5866 (1 row)
11、查詢當(dāng)前session的后臺(tái)服務(wù)進(jìn)程的PID。
postgres=# select pg_backend_pid(); pg_backend_pid ---------------- 3958 (1 row)
12、查看當(dāng)前共享內(nèi)存的大小。
postgres=# show shared_buffers; shared_buffers ---------------- 128MB (1 row)
13、修改當(dāng)前session參數(shù)配置。
postgres=# set maintenance_work_mem to '128MB'; SET postgres=# select set_config('maintenance_work_mem','128MB',false); set_config ------------ 128MB (1 row)
14、查看當(dāng)前正在寫的WAL文件。
postgres=# select pg_xlogfile_name(pg_current_xlog_location()); pg_xlogfile_name -------------------------- 00000001000000000000004B (1 row)
15、查看當(dāng)前WAL的buffer中還有多少字節(jié)的數(shù)據(jù)沒有寫到磁盤中。
postgres=# select pg_xlog_location_diff(pg_current_xlog_insert_location(),pg_current_xlog_location()); pg_xlog_location_diff ----------------------- 0 (1 row)
16、查看數(shù)據(jù)庫(kù)實(shí)例是否正在做基礎(chǔ)備份。
postgres=# select pg_is_in_backup(),pg_backup_start_time(); pg_is_in_backup | pg_backup_start_time -----------------+---------------------- f | (1 row)
17、查看當(dāng)前數(shù)據(jù)庫(kù)實(shí)例時(shí)HOT Standby狀態(tài)還是正常數(shù)據(jù)庫(kù)狀態(tài)。
postgres=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- f (1 row)
18、查看數(shù)據(jù)庫(kù)大小,如果數(shù)據(jù)庫(kù)中有很多表,使用上述命令將比較慢,可能對(duì)當(dāng)前系統(tǒng)產(chǎn)生不利影響,pg_size_pretty()函數(shù)會(huì)把數(shù)字以MB、GB等格式顯示出來。
postgres=# select pg_database_size('postgres'),pg_size_pretty(pg_database_size('postgres')); pg_database_size | pg_size_pretty ------------------+---------------- 67922104 | 65 MB (1 row)
19、查看表的大小,僅計(jì)算表的大小,不包括索引的大小。
postgres=# select pg_size_pretty(pg_relation_size('test')); pg_size_pretty ---------------- 0 bytes (1 row)
20、查看表的大小,pg_total_relation_size()把表上索引的大小也計(jì)算入內(nèi)。
postgres=# select pg_size_pretty(pg_total_relation_size('test')); pg_size_pretty ---------------- 0 bytes (1 row)
21、查看表上所有索引的大小,pg_indexes_size()函數(shù)的參數(shù)名是一個(gè)表對(duì)應(yīng)的oid(輸入表名會(huì)自動(dòng)轉(zhuǎn)換成表的oid),而不是索引的名稱。
postgres=# select pg_size_pretty(pg_indexes_size('test')); pg_size_pretty ---------------- 0 bytes (1 row)
22、查看表空間大小。
postgres=# select pg_size_pretty(pg_tablespace_size('pg_global')); pg_size_pretty ---------------- 477 kB (1 row)
23、查看表對(duì)應(yīng)的數(shù)據(jù)文件。
postgres=# select pg_relation_filepath('test'); pg_relation_filepath ---------------------- base/12902/24952 (1 row)
補(bǔ)充:PostgreSQL命令行常用命令psql
PostgreSQL命令行常用命令(psql)
一般我們使用 psql來和數(shù)據(jù)庫(kù)交互,方括號(hào)中為可選項(xiàng)參數(shù),不帶任何參數(shù)表示連接本機(jī)
psql [option…] [dbname [username]]
登錄數(shù)據(jù)庫(kù)
psql -h 127.0.0.1 -p 5432 -d database -U postgres
-h 數(shù)據(jù)庫(kù)ip
-p 端口號(hào)
-d 數(shù)據(jù)庫(kù)名
-U 登錄用戶名
導(dǎo)入SQL腳本
示例:
psql -U postgres -d database -f sqlScript.sql
將sqlScript.sql導(dǎo)入到名為database的數(shù)據(jù)庫(kù)中
常用命令
展示數(shù)據(jù)庫(kù)
\l 或者 \list
支持正則匹配,例如展示包含post字符的數(shù)據(jù)庫(kù)
\l '*post*'
切換數(shù)據(jù)庫(kù)(創(chuàng)建新的數(shù)據(jù)庫(kù)連接)
\c 可選參數(shù) dbname [ username ] [ host ] [ port ]
eg:
\c postgres 或者 \c postgres username localhost 5432
展示當(dāng)前數(shù)據(jù)庫(kù)下所有關(guān)系(table、view、sequence等)
\d 展示當(dāng)前所有表
\d “Account” 展示Account表字段信息
展示當(dāng)前數(shù)據(jù)庫(kù)下所有schema信息
\dn
顯示當(dāng)前使用的schema
SHOW search_path;
當(dāng)前schema為public
search_path ---------------- "$user",public
切換當(dāng)前schema
SET search_path TO myschema; # set search_path to auth; # SHOW search_path; search_path ------------- auth
斷開數(shù)據(jù)庫(kù)連接
\q
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSql生產(chǎn)級(jí)別數(shù)據(jù)庫(kù)安裝要注意事項(xiàng)
這篇文章主要介紹了PostgreSql生產(chǎn)級(jí)別數(shù)據(jù)庫(kù)安裝要注意事項(xiàng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08postgresql 獲取兩個(gè)時(shí)間類型小時(shí)差值案例
這篇文章主要介紹了postgresql 獲取兩個(gè)時(shí)間類型小時(shí)差值案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12如何使用Dockerfile創(chuàng)建PostgreSQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了如何使用Dockerfile創(chuàng)建PostgreSQL數(shù)據(jù)庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02postgreSQL自動(dòng)生成隨機(jī)數(shù)值的實(shí)例
這篇文章主要介紹了postgreSQL自動(dòng)生成隨機(jī)數(shù)值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL 主備數(shù)據(jù)宕機(jī)恢復(fù)測(cè)試方案
這篇文章主要介紹了PostgreSQL 主備數(shù)據(jù)宕機(jī)恢復(fù)測(cè)試方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL時(shí)間日期的語法及注意事項(xiàng)
在開發(fā)過程中,經(jīng)常要取日期的年,月,日,小時(shí)等值,PostgreSQL 提供一個(gè)非常便利的EXTRACT函數(shù),這篇文章主要給大家介紹了關(guān)于PostgreSQL時(shí)間日期的語法及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2023-01-01PostgreSQL的upsert實(shí)例操作(insert on conflict do)
這篇文章主要介紹了PostgreSQL的upsert實(shí)例操作(insert on conflict do),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01Postgresql 查看SQL語句執(zhí)行效率的操作
這篇文章主要介紹了Postgresql 查看SQL語句執(zhí)行效率的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02