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


当前回答

以超级用户身份登录:

sudo -u postgres psql

您可以通过\l命令列出所有数据库和用户,(通过\?列出其他命令)。

现在,如果您想查看其他数据库,可以通过\c命令(如\c template1、\c postgres postgress)更改用户/数据库,并使用\d、\dt或\dS查看表/视图等。

其他回答

从psql命令行界面,

首先,选择数据库

\c database_name

然后,这将显示当前模式中的所有表:

\dt

编程(当然也可以从psql接口):

SELECT * FROM pg_catalog.pg_tables;

系统表位于pg_catalog数据库中。

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

show tables;

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

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

根据我的口味,在命令行列出所有表的最直接方法是:

psql -a -U <user> -p <port> -h <server> -c "\dt"

对于给定的数据库,只需添加数据库名称:

psql -a -U <user> -p <port> -h <server> -c "\dt" <database_name>

它可以在Linux和Windows上运行。

(为完整起见)

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

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

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

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