在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
其他回答
select
*
from
pg_catalog.pg_tables
where
schemaname != 'information_schema'
and schemaname != 'pg_catalog';
(MySQL)显示当前数据库的表列表
show tables;
(PostgreSQL)显示当前数据库的表列表
select * from pg_catalog.pg_tables where schemaname='public';
您可以使用PostgreSQL的交互式终端Psql来显示PostgreSQL中的表。
1.启动Psql
通常,您可以运行以下命令进入psql:
psql DBNAME USERNAME
例如,psql template1 postgres
您可能遇到的一种情况是:假设您以root用户身份登录,但不记得数据库名称。您可以通过运行以下命令首先进入Psql:
sudo -u postgres psql
在某些系统中,sudo命令不可用,您可以运行以下任一命令:
psql -U postgres
psql --username=postgres
2.显示表格
现在在Psql中,您可以运行以下命令:
\? 列出所有命令\l列出数据库\conninfo显示有关当前连接的信息\c[DBNAME]连接到新数据库,例如\c template1\公共模式的dt列表表\dt<schema name>.*列出特定架构的表,例如\dt public*\dt*.*列出所有模式的表然后可以运行SQL语句,例如SELECT*FROM my_table;(注意:语句必须以分号结尾;)\q退出psql
作为一个快速的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
以超级用户身份登录,以便您可以检查所有数据库及其架构:-
sudo su - postgres
然后,我们可以使用以下命令进入postgresqlshell:-
psql
现在可以使用以下命令检查所有数据库列表:-
\l
如果您想检查数据库的大小,请使用:-
\l+
按q返回。
找到数据库后,现在可以使用以下命令连接到该数据库:-
\c database_name
连接后,您可以通过以下方式检查数据库表或架构:-
\d
现在返回shell使用:-
q
现在,要进一步了解某个表的使用细节:-
\d table_name
要返回postgresql_shell,请按\q。
要返回终端,请按退出。
推荐文章
- 查询JSON类型内的数组元素
- 获得PostgreSQL数据库中当前连接数的正确查询
- 纬度和经度的数据类型是什么?
- 如何在PostgreSQL中临时禁用触发器?
- 输入文件似乎是一个文本格式转储。请使用psql
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何添加列,如果不存在PostgreSQL?
- 如何在Postgres中获得两个字段的MIN() ?
- 截断Postgres数据库中的所有表
- 如何连接列在Postgres选择?
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?
- PostgreSQL:角色不允许登录