PostgreSQL 中的 show tables
(来自 MySQL)的等价物是什么?
从 psql
命令行界面,
首先,选择你的数据库
\c database_name
然后,这将显示当前架构中的所有表:
\dt
以编程方式(当然也可以通过 psql
界面):
SELECT * FROM pg_catalog.pg_tables;
系统表位于 pg_catalog
数据库中。
以超级用户身份登录:
sudo -u postgres psql
您可以通过 \l
命令列出所有数据库和用户,(通过 \?
列出其他命令)。
现在,如果您想查看其他数据库,您可以通过 \c
命令(如 \c template1
、\c postgres postgres
)更改用户/数据库,并使用 \d
、\dt
或 \dS
来查看表/视图/等。
您可以使用 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
(为了完整性)
您还可以查询(SQL 标准)information schema:
SELECT
table_schema || '.' || table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
||
自 1983 年以来一直是 SQL 标准中的字符串连接运算符 - 它是使用非标准字符串连接运算符的 MS SQL。
首先以 postgres 用户身份登录: sudo su - postgres 连接到所需的数据库: psql -d databaseName \dt 将返回您连接到的数据库中所有表的列表。
以超级用户身份登录,以便您可以检查所有数据库及其模式:-
sudo su - postgres
然后我们可以使用以下命令进入 postgresql shell:-
psql
您现在可以使用以下命令检查所有数据库列表:-
\l
如果您还想检查数据库的大小,请使用:-
\l+
按 q
返回。
找到数据库后,您可以使用以下命令连接到该数据库:-
\c database_name
连接后,您可以通过以下方式检查数据库表或架构:-
\d
现在返回到 shell 使用:-
q
现在进一步查看某个表使用的详细信息:-
\d table_name
要返回 postgresql_shell,请按 \q
。
并返回终端按 exit
。
使用 -E 标志运行 psql 将回显内部用于实现 \dt 和类似的查询:
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;
**************************
如果您只想查看已创建的表列表,您可能只会说:
\dt
但我们也有 PATTERN
,它可以帮助您自定义要显示的表格。要显示包括 pg_catalog
在内的所有架构,您可以添加 *
。
\dt *
如果您这样做:\?
\dt[S+] [PATTERN] 列出表格
仅使用查看表格
=> \dt
如果想查看模式表
=>\dt+
如果您想查看特定的架构表
=>\dt schema_name.*
+
与 S
混淆了。后者(字母)显示模式表。 +
仅显示额外信息。
如果您在 PostgreSQL 中使用 pgAdmin4,您可以使用它来显示数据库中的表:
select * from information_schema.tables where table_schema='public';
首先使用以下命令连接数据库
\c database_name
您将看到这条消息 - You are now connected to database database_name
。他们运行以下命令
SELECT * FROM table_name;
在 database_name 和 table_name 中,只需更新您的数据库和表名
请注意,仅 \dt
将列出您正在使用的数据库的 public 架构中的表。我喜欢将我的表保存在单独的模式中,所以接受的答案对我不起作用。
要列出特定模式中的所有表,我需要:
1)连接到所需的数据库:
psql mydb
2) 在 \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';
\dt (no * required) -- 将列出您已经连接到的现有数据库的所有表。还需要注意:
\d [table_name] -- 将显示给定表的所有列,包括类型信息、引用和键约束。
您可以使用 \dt
列出当前数据库中的表。
Fwiw,\d tablename
将显示有关给定表的详细信息,类似于 MySQL 中的 show columns from tablename
,但包含更多信息。
根据我的口味,在命令行列出所有表的最直接方法是:
psql -a -U <user> -p <port> -h <server> -c "\dt"
对于给定的数据库,只需添加数据库名称:
psql -a -U <user> -p <port> -h <server> -c "\dt" <database_name>
它适用于 Linux 和 Windows。
这些步骤对我有用 PostgreSQL 13.3
和 Windows 10
打开 cmd 并输入 psql -a -U [username] -p [port] -h [server] 输入 \c [database] 以连接到数据库 输入 \dt 或 \d 以显示所有表
\dt
将列出表格,而 \pset pager off
在同一窗口中显示它们,而不会切换到单独的窗口。爱死 dbshell 中的那个特性。
使用 psql : \dt
或者:
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 查询适用于 PostgreSQL 的大多数版本并且相当简单。
select table_name from information_schema.tables where table_schema='public' ;
首先你必须连接你的数据库,比如
我的数据库是 ubuntu
使用此命令连接
\c ubuntu
此消息将显示
“您现在以用户“postgres”的身份连接到数据库“ubuntu”。”
现在
运行此命令以显示其中的所有表
\d+
作为一个快速的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 输出 multi-liner :
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
登录后在 PostgreSQL 命令行界面中,键入以下命令以连接所需的数据库。 \c [数据库名称]
然后您将看到此消息 You are now connected to database "[database_name]"
键入以下命令以列出所有表。 \dt
(MySQL) 显示当前数据库的表列表
show tables;
(PostGreSQL) 显示当前数据库的表列表
select * from pg_catalog.pg_tables where schemaname='public';
显示当前数据库的表列表
选择 * 从 pg_catalog.pg_tables;
要在 psql 中查看外部表,请运行 \dE
首先,您可以使用 mac 上的 postgre.app 或 postico 连接您的 postgres 数据库。运行以下命令:
psql -h localhost -p port_number -d database_name -U user_name -W
然后你输入你的密码,这应该可以访问你的数据库
不定期副业成功案例分享
\l
相当于 MySQL 中的show databases
。dt
≃show tables
和l
≃show databases
\dt
非常有用。pg_catalog.pg_tables
的情况要少得多,因为它似乎将内部表与您碰巧连接到的任何数据库的用户创建的表混为一谈。psql my_db_name
以使\dt
工作。当我在没有数据库名称的情况下运行psql
时,我收到“未找到关系”消息SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'
\c <DATABASE_NAME>
来选择您的数据库。