遠程登錄sql
psql -h 主機 -U 用戶 -p端口 -W -d 數據庫 #-W表示密碼,例如:psql -h 192.168.137.3 -U postgres -p 5432 -W -d postgres
遠程執行命令數據庫
psql -h 192.168.137.3 -p 5432 postgres postgres -A -c "select * from test limit 10"
查看全部數據庫vim
\l
切換數據庫session
\c postgres
查看當前庫裏面的全部表工具
\dt
查看錶結構post
\d test
顯示全部用戶spa
\du
查看當前用戶和當前庫code
#查看當前用戶 postgres=> select user; user -------- dbuser (1 row) #查看當前庫 postgres=> select current_database(); current_database ------------------ postgres (1 row)
賦予全部用戶對錶的全部權限server
grant all on test to public;
注意:blog
能夠建大寫的表名可是要加上雙引號,並且使用工具navicat點擊的時候顯示沒有權限,可是能夠經過命令查看
常常執行postgres命令的時候會雜亂顯示,能夠設置豎排查看
\x
定製命令,進入數據庫使用 :命令 查詢
vim ~/.psqlrc #查詢等待事件 \set wait_event 'select pid, usename, datname, query, client_addr from pg_stat_activity where pid <> pg_backend_pid() and wait_event is not null order by wait_event_type;' #查詢數據庫鏈接數 \set connections_jqs 'select usename, datname, client_addr, count(*) from pg_stat_activity where pid <> pg_backend_pid() group by 1,2,3 order by 1,2,4 desc;' #查詢活動會話 \set active_session_jqs 'select pid, usename, datname, query, client_addr from pg_stat_activity where pid <> pg_backend_pid() and state=\' active\' order by query;'
定製命令使用:
postgres=# :active_session_jqs; pid | usename | datname | query | client_addr -----+---------+---------+-------+------------- (0 rows) postgres=# :wait_event; pid | usename | datname | query | client_addr ------+----------+----------+--------------------------------------------------------------------------+--------------- 7136 | | | | 7138 | postgres | | | 7134 | | | | 7133 | | | | 7135 | | | | 8400 | postgres | postgres | +| 192.168.137.1 | | | SELECT +| | | | db.oid as did, db.datname, db.datallowconn, +| | | | pg_encoding_to_char(db.encoding) AS serverencoding, +| | | | has_database_privilege(db.oid, 'CREATE') as cancreate, datlastsysoid+| | | | FROM +| | | | pg_database db +| | | | WHERE db.oid = 13287 | (6 rows) postgres=# :connections_jps; ERROR: syntax error at or near ":" LINE 1: :connections_jps; ^ postgres=# :connections_jqs; #沒弄明白爲啥第二次能夠得出結果 usename | datname | client_addr | count ----------+----------+---------------+------- postgres | postgres | 192.168.137.1 | 1 postgres | | | 1 | | | 4 (3 rows)