在PostgreSQL中显示表(来自MySQL)的等价物是什么?


当前回答

作为一个快速的oneliner

# just list all the postgres tables sorted in the terminal
db='my_db_name'
clear;psql -d $db -t -c '\dt'|cut -c 11-|perl -ne 's/^([a-z_0-9]*)( )(.*)/$1/; print'

或者如果您更喜欢更清晰的json输出多行:

IFS='' read -r -d '' sql_code <<"EOF_CODE"
    select array_to_json(array_agg(row_to_json(t))) from (
        SELECT table_catalog,table_schema,table_name 
        FROM information_schema.tables
        ORDER BY table_schema,table_name ) t
EOF_CODE
psql -d postgres -t -q -c "$sql_code"|jq

其他回答

select 
  * 
from 
  pg_catalog.pg_tables 
where 
  schemaname != 'information_schema' 
  and schemaname != 'pg_catalog';

作为一个快速的oneliner

# just list all the postgres tables sorted in the terminal
db='my_db_name'
clear;psql -d $db -t -c '\dt'|cut -c 11-|perl -ne 's/^([a-z_0-9]*)( )(.*)/$1/; print'

或者如果您更喜欢更清晰的json输出多行:

IFS='' read -r -d '' sql_code <<"EOF_CODE"
    select array_to_json(array_agg(row_to_json(t))) from (
        SELECT table_catalog,table_schema,table_name 
        FROM information_schema.tables
        ORDER BY table_schema,table_name ) t
EOF_CODE
psql -d postgres -t -q -c "$sql_code"|jq

显示当前数据库的表列表

从pg_catalog.pg_tables中选择*;

(MySQL)显示当前数据库的表列表

show tables;

(PostgreSQL)显示当前数据库的表列表

select * from pg_catalog.pg_tables where schemaname='public';

\dt(不需要*)--将列出您已连接到的现有数据库的所有表。还需要注意:

\d[table_name]--将显示给定表的所有列,包括类型信息、引用和键约束。