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


当前回答

以超级用户身份登录,以便您可以检查所有数据库及其架构:-

sudo su - postgres

然后,我们可以使用以下命令进入postgresqlshell:-

psql

现在可以使用以下命令检查所有数据库列表:-

\l

如果您想检查数据库的大小,请使用:-

\l+

按q返回。

找到数据库后,现在可以使用以下命令连接到该数据库:-

\c database_name

连接后,您可以通过以下方式检查数据库表或架构:-

\d

现在返回shell使用:-

q

现在,要进一步了解某个表的使用细节:-

\d table_name

要返回postgresql_shell,请按\q。

要返回终端,请按退出。

其他回答

如果您在PostgreSQL中使用pgAdmin4,可以使用它来显示数据库中的表:

select * from information_schema.tables where table_schema='public';

运行带有-E标志的psql将响应内部用于实现的查询\dt和类似:

sudo -u postgres psql -E

postgres=# \dt       
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name", 
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','')
    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;        
**************************

(为完整起见)

您还可以查询(SQL标准)信息架构:

SELECT
    table_schema || '.' || table_name
FROM
    information_schema.tables
WHERE
    table_type = 'BASE TABLE'
AND
    table_schema NOT IN ('pg_catalog', 'information_schema');

首次以postgres用户身份登录:sudo su-后期连接到所需的数据库:psql-d databaseName\dt将返回连接到的数据库中所有表的列表。

作为一个快速的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