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


当前回答

请注意,仅\dt将列出您正在使用的数据库的公共模式中的表。我喜欢将表保存在单独的模式中,因此接受的答案对我来说不起作用。

要列出特定模式中的所有表,我需要:

1) 连接到所需的数据库:

psql mydb

2) 指定要在\dt命令后查看表的架构名称,如下所示:

\dt myschema.*

这显示了我感兴趣的结果:

               List of relations
 Schema   |       Name      | Type  |  Owner   
----------+-----------------+-------+----------
 myschema | users           | table | postgres
 myschema | activity        | table | postgres
 myschema | roles           | table | postgres

其他回答

首先,您可以使用mac上的postgre.app或postico连接postgres数据库。运行以下命令:

psql -h localhost -p port_number -d database_name -U user_name -W

然后输入密码,这应该可以访问数据库

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

show tables;

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

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

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

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

该SQL查询适用于大多数PostgreSQL版本,非常简单。

select table_name 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;        
**************************