PostgreSQL快捷命令SQL

2021-10-10 17:38:19 字數 4659 閱讀 3008

\l+

檢視所有資料庫

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",

case when pg_catalog.has_database_privilege(d.datname, 'connect')

then pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))

else 'no access'

end as "size",

t.spcname as "tablespace",

pg_catalog.shobj_description(d.oid, 'pg_database') as "description"

from pg_catalog.pg_database d

join pg_catalog.pg_tablespace t on d.dattablespace = t.oid

order by 1;

\d+

檢視資料庫表、檢視、索引、序列等資訊

select n.nspname as "schema",

c.relname as "name",

case c.relkind when 'r' then 'table' when 'v' then 'view' when 'm' then 'materialized view' when 'i' then 'index' when 's' then 'sequence' when 's' then 'special' when 'f' then 'foreign table' when 'p' then 'partitioned table' when 'i' then 'partitioned index' end as "type",

pg_catalog.pg_get_userbyid(c.relowner) as "owner",

pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as "size",

pg_catalog.obj_description(c.oid, 'pg_class') as "description"

from pg_catalog.pg_class c

left join pg_catalog.pg_namespace n on n.oid = c.relnamespace

where c.relkind in ('r','p','v','m','s','f','')

and n.nspname <> 'pg_catalog'

and n.nspname <> 'information_schema'

and n.nspname !~ '^pg_toast'

and pg_catalog.pg_table_is_visible(c.oid)

order by 1,2;

\du+

列出所有的資料庫使用者和角色

select r.rolname, r.rolsuper, r.rolinherit,

r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,

r.rolconnlimit, r.rolvaliduntil,

array(select b.rolname

from pg_catalog.pg_auth_members m

join pg_catalog.pg_roles b on (m.roleid = b.oid)

where m.member = r.oid) as memberof

, pg_catalog.shobj_description(r.oid, 'pg_authid') as description

, r.rolreplication

, r.rolbypassrls

from pg_catalog.pg_roles r

where r.rolname !~ '^pg_'

order by 1;

\db+

表空間資訊

select spcname as "name",

pg_catalog.pg_get_userbyid(spcowner) as "owner",

pg_catalog.pg_tablespace_location(oid) as "location",

pg_catalog.array_to_string(spcacl, e'\n') as "access privileges",

spcoptions as "options",

pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) as "size",

pg_catalog.shobj_description(oid, 'pg_tablespace') as "description"

from pg_catalog.pg_tablespace

order by 1;

\dp+

顯示表的許可權分配情況

select n.nspname as "schema",

c.relname as "name",

case c.relkind when 'r' then 'table' when 'v' then 'view' when 'm' then 'materialized view' when 's' then 'sequence' when 'f' then 'foreign table' when 'p' then 'partitioned table' end as "type",

pg_catalog.array_to_string(c.relacl, e'\n') as "access privileges",

pg_catalog.array_to_string(array(

select attname || e':\n ' || pg_catalog.array_to_string(attacl, e'\n ')

from pg_catalog.pg_attribute a

where attrelid = c.oid and not attisdropped and attacl is not null

), e'\n') as "column privileges",

pg_catalog.array_to_string(array(

select polname

|| case when not polpermissive then

e' (restrictive)'

else '' end

|| case when polcmd != '*' then

e' (' || polcmd || e'):'

else e':'

end|| case when polqual is not null then

e'\n (u): ' || pg_catalog.pg_get_expr(polqual, polrelid)

else e''

end|| case when polwithcheck is not null then

e'\n (c): ' || pg_catalog.pg_get_expr(polwithcheck, polrelid)

else e''

end || case when polroles <> '' then

e'\n to: ' || pg_catalog.array_to_string(

array(

select rolname

from pg_catalog.pg_roles

where oid = any (polroles)

order by 1

), e', ')

else e''

endfrom pg_catalog.pg_policy pol

where polrelid = c.oid), e'\n')

as "policies"

from pg_catalog.pg_class c

left join pg_catalog.pg_namespace n on n.oid = c.relnamespace

where c.relkind in ('r','v','m','s','f','p')

and n.nspname !~ '^pg_' and pg_catalog.pg_table_is_visible(c.oid)

order by 1, 2;

常用postgresql命令

1.初始化postgresql資料庫 方法1 usr pgsql 9.4 bin initdb d opt pgsql data 方法2 sudo service postgresql initdb 2.啟動postgresql服務 sudo service postgresql start sta...

postgresql的Explain命令結果分析

網上檢視了一些對explain命令的結果分析,但是要不就是不清楚,要不就是一大堆,所以特意看了官方文件,並記錄如下 語法如下 explain sql語句 摘取結果如圖 下面是引用官方文件 1 預計啟動時間 如果每一行都被檢索了,則為0 2 預計執行計畫的總行數 3 此計畫節點輸出的行的平均寬度 以位...

PostgreSQL 元命令介紹

postgresql中的元命令是指以 反斜線 開通的命令,怕失去了,提供的豐富的元命令,能夠便捷地管理資料庫,比如檢視資料庫物件的定義,檢視資料庫物件占用空間的大小,列出資料庫各種物件名稱,暑假匯入匯出等,比如檢視資料庫列表,如下所示 1.使用元命令檢視表空間 db,如下所示 2.檢視表的定義 d,...