在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

其他回答


使用psql:\dt

Or:

SELECT c.relname AS Tables_in FROM pg_catalog.pg_class c
        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE pg_catalog.pg_table_is_visible(c.oid)
        AND c.relkind = 'r'
        AND relname NOT LIKE 'pg_%'
ORDER BY 1

(为完整起见)

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

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

首先,您必须连接数据库

我的数据库是ubuntu

使用此命令连接

 \c ubuntu

此消息将显示

您现在以用户“postgres”的身份连接到数据库“ubuntu”

Now

运行此命令以显示其中的所有表

\d+

如果您只想查看已创建的表列表,可以只说:

\dt

但我们也有PATTERN,它将帮助您定制要显示的表。要显示包括pg_catalog架构在内的所有架构,可以添加*。

\时间*

如果您这样做:\?

\dt[S+][PATTERN]列表表

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