如何在 PostgreSQL 中显示表?

发布于 2024-07-17 07:53:57 字数 61 浏览 10 评论 0原文

PostgreSQL 中的 show table (来自 MySQL)相当于什么?

What's the equivalent to show tables (from MySQL) in PostgreSQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(29

み格子的夏天 2024-07-24 07:53:57

psql 命令行界面,

首先选择您的数据库

\c database_name

然后,这将显示当前架构中的所有表:

\dt

以编程方式(当然,也可以从 psql 界面):

SELECT * FROM pg_catalog.pg_tables;

系统表位于 pg_catalog 数据库中。

From the psql command line interface,

First, choose your database

\c database_name

Then, this shows all tables in the current schema:

\dt

Programmatically (or from the psql interface too, of course):

SELECT * FROM pg_catalog.pg_tables;

The system tables live in the pg_catalog database.

刘备忘录 2024-07-24 07:53:57

您可以使用 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 中,您可以运行以下命令:

  1. \? 列出所有命令
  2. \l 列出数据库
  3. \conninfo 显示有关当前连接的信息
  4. \c [DBNAME] 连接到新数据库,例如 \c template1
  5. \dt 列出公共模式的表
  6. \dt.* 列出某个 schema 的表,例如 \dt public.*
  7. \dt *.* 列出以下的表all schemas
  8. 然后就可以运行SQL语句了,例如SELECT * FROM my_table;(注意:语句必须以分号;结束)
  9. \q 退出psql

You can use PostgreSQL's interactive terminal Psql to show tables in PostgreSQL.

1. Start Psql

Usually you can run the following command to enter into psql:

psql DBNAME USERNAME

For example, psql template1 postgres

One situation you might have is: suppose you login as root, and you don't remember the database name. You can just enter first into Psql by running:

sudo -u postgres psql

In some systems, sudo command is not available, you can instead run either command below:

psql -U postgres
psql --username=postgres

2. Show tables

Now in Psql you could run commands such as:

  1. \? list all the commands
  2. \l list databases
  3. \conninfo display information about current connection
  4. \c [DBNAME] connect to new database, e.g., \c template1
  5. \dt list tables of the public schema
  6. \dt <schema-name>.* list tables of certain schema, e.g., \dt public.*
  7. \dt *.* list tables of all schemas
  8. Then you can run SQL statements, e.g., SELECT * FROM my_table;(Note: a statement must be terminated with semicolon ;)
  9. \q quit psql
虫児飞 2024-07-24 07:53:57

以超级用户身份登录:

sudo -u postgres psql

您可以通过\l命令列出所有数据库和用户,(通过\?列出其他命令)。

现在,如果您想查看其他数据库,您可以通过 \c 命令更改用户/数据库,例如 \c template1\c postgres postgres 并使用\d\dt\dS 查看表/视图/等。

Login as superuser:

sudo -u postgres psql

You can list all databases and users by \l command, (list other commands by \?).

Now if you want to see other databases you can change user/database by \c command like \c template1, \c postgres postgres and use \d, \dt or \dS to see tables/views/etc.

与他有关 2024-07-24 07:53:57

(为了完整性)

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

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

(For completeness)

You could also query the (SQL-standard) 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');
巴黎夜雨 2024-07-24 07:53:57

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

sudo su - postgres

然后我们可以使用以下命令进入 postgresql shell:-

psql

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

\l

如果您愿意检查数据库的大小并使用:-

\l+

q 返回。

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

\c database_name

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

\d

现在返回到 shell 使用:-

q

现在进一步查看数据库的详细信息某些表使用:-

\d table_name

要返回 postgresql_shell,请按 \q

要返回终端,请按 exit

Login as a superuser so that you can check all the databases and their schemas:-

sudo su - postgres

Then we can get to postgresql shell by using following command:-

psql

You can now check all the databases list by using the following command:-

\l

If you would like to check the sizes of the databases as well use:-

\l+

Press q to go back.

Once you have found your database now you can connect to that database using the following command:-

\c database_name

Once connected you can check the database tables or schema by:-

\d

Now to return back to the shell use:-

q

Now to further see the details of a certain table use:-

\d table_name

To go back to postgresql_shell press \q.

And to return back to terminal press exit.

万人眼中万个我 2024-07-24 07:53:57
  1. 首次以 postgres 用户身份登录:

    sudo su - postgres

  2. 连接到所需的数据库:psql -d databaseName

  3. \dt 将返回您数据库中所有表的列表重新连接到。

  1. First login as postgres user:

    sudo su - postgres

  2. connect to the required db: psql -d databaseName

  3. \dt would return the list of all table in the database you're connected to.

等风来 2024-07-24 07:53:57

使用 -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;        
**************************

Running psql with the -E flag will echo the query used internally to implement
\dt and similar:

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;        
**************************
ヅ她的身影、若隐若现 2024-07-24 07:53:57

(MySQL) 显示当前数据库的表列表

show tables;

(PostgreSQL) 显示当前数据库的表列表

select * from pg_catalog.pg_tables where schemaname='public';

(MySQL) shows tables list for current database

show tables;

(PostgreSQL) shows tables list for current database

select * from pg_catalog.pg_tables where schemaname='public';
甜柠檬 2024-07-24 07:53:57

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

\dt

但我们还有 PATTERN 它将帮助您自定义要显示的表格。 要显示所有包括 pg_catalog 架构,您可以添加 *

\dt *

如果您这样做:\?

\dt[S+] [PATTERN] 列表

If you only want to see the list of tables you've created, you may only say:

\dt

But we also have PATTERN which will help you customize which tables to show. To show all including pg_catalog Schema, you can add *.

\dt *

If you do: \?

\dt[S+] [PATTERN] list tables

感情旳空白 2024-07-24 07:53:57

请使用仅查看表

=> \dt

如果要查看

=>\dt+

特定架构表,

=>\dt schema_name.* 

use only see a tables

=> \dt

if want to see schema tables

=>\dt+

if you want to see specific schema tables

=>\dt schema_name.* 
丑疤怪 2024-07-24 07:53:57

如果您在 PostgreSQL 中使用 pgAdmin4,您可以使用它来显示数据库中的表:

select * from information_schema.tables where table_schema='public';

If you are using pgAdmin4 in PostgreSQL, you can use this to show the tables in your database:

select * from information_schema.tables where table_schema='public';
苦行僧 2024-07-24 07:53:57

首先使用以下命令连接数据库

\c database_name

,您将看到此消息 - 您现在已连接到数据库database_name。 他们运行以下命令

SELECT * FROM table_name;

在database_name和table_name中只需更新您的数据库和表名称

First Connect with the Database using following command

\c database_name

And you will see this message - You are now connected to database database_name. And them run the following command

SELECT * FROM table_name;

In database_name and table_name just update with your database and table name

花辞树 2024-07-24 07:53:57
select 
  * 
from 
  pg_catalog.pg_tables 
where 
  schemaname != 'information_schema' 
  and schemaname != 'pg_catalog';
select 
  * 
from 
  pg_catalog.pg_tables 
where 
  schemaname != 'information_schema' 
  and schemaname != 'pg_catalog';
渔村楼浪 2024-07-24 07:53:57

请注意,\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

Note that \dt alone will list tables in the public schema of the database you're using. I like to keep my tables in separate schemas, so the accepted answer didn't work for me.

To list all tables within a specific schema, I needed to:

1) Connect to the desired database:

psql mydb

2) Specify the schema name I want to see tables for after the \dt command, like this:

\dt myschema.*

This shows me the results I'm interested in:

               List of relations
 Schema   |       Name      | Type  |  Owner   
----------+-----------------+-------+----------
 myschema | users           | table | postgres
 myschema | activity        | table | postgres
 myschema | roles           | table | postgres
等风来 2024-07-24 07:53:57

这些步骤适用于 PostgreSQL 13.3Windows 10

  1. 打开 cmd 并输入 psql -a -U [用户名] -p [端口] -h [服务器]
  2. 输入 \c [database] 连接到数据库
  3. 输入 \dt\d 显示所有表

Those steps worked for me with PostgreSQL 13.3 and Windows 10

  1. Open cmd and type psql -a -U [username] -p [port] -h [server]
  2. Type \c [database] to connect to the database
  3. Type \dt or \d to show all tables
待"谢繁草 2024-07-24 07:53:57

\dt 将列出表格,而 \pset pager off 在同一窗口中显示它们,而无需切换到单独的窗口。 非常喜欢 dbshel​​l 中的这个功能。

\dt will list tables, and \pset pager off shows them in the same window, without switching to a separate one. Love that feature to death in dbshell.

兔姬 2024-07-24 07:53:57

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

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

\dt (no * required) -- will list all tables for an existing database you are already connected to. Also useful to note:

\d [table_name] -- will show all columns for a given table including type information, references and key constraints.

逆流 2024-07-24 07:53:57

此 SQL 查询适用于大多数版本的 PostgreSQL,并且相当简单。

select table_name from information_schema.tables where table_schema='public' ;

This SQL Query works with most of the versions of PostgreSQL and fairly simple .

select table_name from information_schema.tables where table_schema='public' ;
感情洁癖 2024-07-24 07:53:57

根据我的口味,在命令行列出所有表的最直接方法是:

psql -a -U <user> -p <port> -h <server> -c "\dt"

对于给定的数据库,只需添加数据库名称:

psql -a -U <user> -p <port> -h <server> -c "\dt" <database_name>

它适用于 Linux 和 Windows。

The most straightforward way to list all tables at command line is, for my taste :

psql -a -U <user> -p <port> -h <server> -c "\dt"

For a given database just add the database name :

psql -a -U <user> -p <port> -h <server> -c "\dt" <database_name>

It works on both Linux and Windows.

梦巷 2024-07-24 07:53:57

这些列出了当前数据库的所有模式的所有表:

\dt *.*
\dtS *.*

这些详细列出了当前数据库的所有模式的所有表:

\dt+ *.*
\dtS+ *.*

这些列出了 pg_catalogpublic 当前数据库的模式:

\dtS
\dtS *
\dt *

这些列出了 pg_catalogpublic 架构详细信息:

\dtS+
\dtS+ *
\dt+ *

这列出了当前数据库的 public 架构的所有表:

\dt

这列出了 public 的所有表当前数据库架构的详细信息:

\dt+

这些列出了当前数据库的 my_schema 架构的所有表:

\dtS my_schema.*
\dt my_schema.*

这些详细列出了当前数据库的 my_schema 架构的所有表:

\dtS+ my_schema.*
\dt+ my_schema.*

These list all tables of all schemas of the current database:

\dt *.*
\dtS *.*

These list all tables of all schemas of the current database in detail:

\dt+ *.*
\dtS+ *.*

These list all tables of pg_catalog and public schemas of the current database:

\dtS
\dtS *
\dt *

These list all tables of pg_catalog and public schemas of the current database in detail:

\dtS+
\dtS+ *
\dt+ *

This lists all tables of public schema of the current database:

\dt

This lists all tables of public schema of the current database in detail:

\dt+

These list all tables of my_schema schema of the current database:

\dtS my_schema.*
\dt my_schema.*

These list all tables of my_schema schema of the current database in detail:

\dtS+ my_schema.*
\dt+ my_schema.*
铁轨上的流浪者 2024-07-24 07:53:57

您可以使用\dt列出当前数据库中的表。

Fwiw,\d tablename 将显示给定表的详细信息,类似于 MySQL 中的show columns from tablename,但有更多信息。

You can list the tables in the current database with \dt.

Fwiw, \d tablename will show details about the given table, something like show columns from tablename in MySQL, but with a little more information.

贪恋 2024-07-24 07:53:57
  1. 登录后在 PostgreSQL 命令行界面中,键入以下命令连接所需的数据库。

    <前><代码> \c [数据库名称]

然后您将看到此消息您现在已连接到数据库“[database_name]”

  1. 键入以下命令以列出所有表。

    <前><代码> \dt

  1. In PostgreSQL command-line interface after login, type the following command to connect with the desired database.

        \c [database_name]
    

Then you will see this message You are now connected to database "[database_name]"

  1. Type the following command to list all the tables.

        \dt
    
战皆罪 2024-07-24 07:53:57

作为“快速单线”

# how-to list all the tables 
export PGUSER='postgres'
export PGHOST='postgres-host-end-point'
export PGPORT=5432
export PGDATABASE=foobar

PGPASSWORD='uber-secret' psql -d $PGDATABASE -t -q -c \
 "SELECT table_catalog,table_schema,table_name 
   FROM information_schema.tables where table_schema='public';

或者如果您更喜欢更清晰的 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

as a "quick oneliner"

# how-to list all the tables 
export PGUSER='postgres'
export PGHOST='postgres-host-end-point'
export PGPORT=5432
export PGDATABASE=foobar

PGPASSWORD='uber-secret' psql -d $PGDATABASE -t -q -c \
 "SELECT table_catalog,table_schema,table_name 
   FROM information_schema.tables where table_schema='public';

or if you prefer much clearer json output 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
落花浅忆 2024-07-24 07:53:57

使用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

Using 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
梦回梦里 2024-07-24 07:53:57

首先,您必须连接数据库,就像

我的数据库是 ubuntu

使用此命令进行连接

 \c ubuntu

此消息将显示

“您现在已作为用户“postgres”连接到数据库“ubuntu”。”

现在

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

\d+

First of all you have to connect with your database like

my database is ubuntu

use this command to connect

 \c ubuntu

This message will show

"You are now connected to database "ubuntu" as user "postgres"."

Now

Run this command to show all tables in it

\d+
瑾夏年华 2024-07-24 07:53:57

\dt 可以工作。 它的等价物是

SELECT
  n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' 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', 'p', '')
  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 will work. And the equivalence of it is

SELECT
  n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' 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', 'p', '')
  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;
凝望流年 2024-07-24 07:53:57

要在 psql 中查看外部表,请运行 \dE

To view foreign tables in psql, run \dE

他不在意 2024-07-24 07:53:57

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

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

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

First you can connect with your postgres database using the postgre.app on mac or using postico.
Run the following command:

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

then you enter your password, this should give access to your database

岁月苍老的讽刺 2024-07-24 07:53:57

另请注意 pgAdmin 文档 中的这一点:

pgAdmin 4 提供了允许您修改所有表特性和属性的对话框。

要访问允许您创建数据库对象的对话框,请右键单击 pgAdmin 树控件中的对象类型,然后选择该对象的“创建”选项。 例如,要创建新表,请从树形控件中选择一个数据库,选择该数据库下的架构,右键单击“表”节点,然后选择“创建表”。

因此,一旦您完成了该操作(或者您也可以使用 SQL 命令和 pSQL 客户端)并且您有了一些表,您就可以通过在 pgAdmin UI 中展开“Schemas”对象来查看它们。 下面是它的屏幕截图:

pgAdmin 4 UI 中的表格

选择表格后,您还可以点击顶部工具栏(带有网格表格图标的工具栏)的“查看数据”来查看该表格的记录。

Also note this from the pgAdmin documentation:

pgAdmin 4 provides dialogs that allow you to modify all table properties and attributes.

To access a dialog that allows you to create a database object, right-click on the object type in the pgAdmin tree control, and select the Create option for that object. For example, to create a new table, Select a database from the tree control, select the schema under the database, right-click on the Tables node, and select Create Table.

So once you've done that (or you could also use SQL commands and the pSQL client) and you have some tables, you can view them by expanding the "Schemas" object in the pgAdmin UI. Here's a screenshot of what it looks like:

Tables in pgAdmin 4 UI

After selecting a table, you may also click on "View Data" at the top toolbar (the one with the grid table icon) to view the table's records.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文