在PostgreSQL中,与 "显示表"(来自MySQL)相当的是什么?
从psql
命令行界面。
首先,选择你的数据库
\c database_name
然后,这将显示当前模式下的所有表。
\dt
以编程方式(当然也可以从psql
界面)。
SELECT * FROM pg_catalog.pg_tables;
系统表存在于pg_catalog
数据库中。
(为完整起见)
你也可以查询(SQL标准的)[信息模式](http://www.postgresql.org/docs/current/static/information-schema.html)。
SELECT
table_schema || '.' || table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
你可以使用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模板1
。
`/dt'公共模式清单表
/dt <schema-name>.*
列出某些schema的表格,如/dt public.*
。
dt/dt *.*
列出所有模式的表。
然后就可以运行SQL语句,例如:SELECT * FROM my_table;
(注意。
语句必须以分号;
结束。)
q
退出psql使用-E标志运行psql,将呼应内部使用的查询,以实现 \ddt和类似。
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;
**************************
以超级用户的身份登录,这样你就可以检查所有的数据库和它们的模式:-。
sudo su - postgres
然后我们可以通过以下命令进入postgresql shell:-。
psql
现在您可以使用以下命令检查所有的数据库列表:-。
\l
如果您想检查数据库的大小,请使用:-。
\l+
按q
返回。
一旦你找到了你的数据库,现在你就可以使用下面的命令连接到该数据库了。
\c database_name
一旦连接,您可以通过以下方式检查数据库表或模式:----。
\d
现在回到shell,使用:-。
q
现在要进一步查看某个表格的详细信息,请使用:-。
\d table_name
要返回postgresql_shell,请按q
。
如果要返回到终端,请按 "exit"。
请注意,\dt
就会列出你正在使用的数据库的公共模式中的表。
我喜欢把我的表放在单独的模式中,所以公认的答案对我不起作用。
要列出*在特定模式中的所有表,我需要。
psql mydb
\dt
命令后面指定我想看到的表的模式名称,比如这样。\dt myschema.*
这让我看到了我感兴趣的结果'。
List of relations
Schema | Name | Type | Owner
----------+-----------------+-------+----------
myschema | users | table | postgres
myschema | activity | table | postgres
myschema | roles | table | postgres
select
*
from
pg_catalog.pg_tables
where
schemaname != 'information_schema'
and schemaname != 'pg_catalog';