在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

其他回答

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

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

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

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

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

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

首先,您必须连接数据库

我的数据库是ubuntu

使用此命令连接

 \c ubuntu

此消息将显示

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

Now

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

\d+

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

sudo su - postgres

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

psql

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

\l

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

\l+

按q返回。

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

\c database_name

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

\d

现在返回shell使用:-

q

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

\d table_name

要返回postgresql_shell,请按\q。

要返回终端,请按退出。