Jon*_*nas 1362 postgresql tools psql command-line
我正在尝试学习 PostgreSQL 管理并开始学习如何使用psql命令行工具。
当我使用 登录时psql --username=postgres,如何列出所有数据库和表?
我曾尝试\d,d并dS+但没有列出。我已经用 pgAdmin III 创建了两个数据库和几个表,所以我知道它们应该被列出。
Rol*_*DBA 428
这列出了数据库:
SELECT datname FROM pg_database
WHERE datistemplate = false;
Run Code Online (Sandbox Code Playgroud)
这列出了当前数据库中的表
SELECT table_schema,table_name
FROM information_schema.tables
ORDER BY table_schema,table_name;
Run Code Online (Sandbox Code Playgroud)
Eri*_*ski 117
el@defiant$ /bin/psql -h localhost --username=pgadmin --list
Run Code Online (Sandbox Code Playgroud)
或者命令更简单地说:
psql -U pgadmin -l
Run Code Online (Sandbox Code Playgroud)
这些命令在终端上打印:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
kurz_prod | pgadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
pgadmin | pgadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
Run Code Online (Sandbox Code Playgroud)
这些是可用的数据库。
您必须先指定一个数据库,然后才能列出该数据库中的表。
el@defiant$ psql -U pgadmin -d kurz_prod
Run Code Online (Sandbox Code Playgroud)
这将带您进入 psql 终端:
kurz_prod=#
Run Code Online (Sandbox Code Playgroud)
使用命令的\d意思是显示所有表、视图和序列
kurz_prod=# \d
Run Code Online (Sandbox Code Playgroud)
这打印:
List of relations
Schema | Name | Type | Owner
--------+---------+----------+---------
public | mytable | table | pgadmin
public | testing | sequence | pgadmin
(2 rows)
Run Code Online (Sandbox Code Playgroud)
然后,要退出 psql 终端,请键入\q并按 Enter。或者Ctrl-D做同样的事情。这些是该数据库中的表。
Son*_*riz 38
要获得有关数据库和表列表的更多信息,您可以执行以下操作:
\l+ 列出数据库
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
------------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
pgbench | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 29 MB | pg_default |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 6073 kB | pg_default | default administrative connection database
slonmaster | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 1401 MB | movespace |
slonslave | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 32 MB | pg_default |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| 5785 kB | pg_default | unmodifiable empty database
| | | | | postgres=CTc/postgres | | |
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| 5985 kB | pg_default | default template for new databases
| | | | | postgres=CTc/postgres | | |
test | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 13 MB | pg_default |
(7 rows)
Run Code Online (Sandbox Code Playgroud)
和
\d+ 列出当前数据库中当前 search_path 模式中的所有表。
test=# \dn+ --list schemas
List of schemas
Name | Owner | Access privileges | Description
--------+----------+----------------------+------------------------
public | postgres | postgres=UC/postgres+| standard public schema
| | =UC/postgres |
schema1 | postgres | postgres=UC/postgres+|
| | =UC/postgres |
(2 row)
test=# set search_path to schema1, public;
SET
test=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
---------+-----------------+-------+--------------+------------+-------------
public | all_units | table | postgres | 0 bytes |
public | asset | table | postgres | 16 kB |
public | asset_attribute | table | postgres | 8192 bytes |
public | food | table | postgres | 48 kB |
public | name_log | table | postgres | 8192 bytes |
public | outable | table | ordinaryuser | 0 bytes |
public | outable2 | table | ordinaryuser | 0 bytes |
public | test | table | postgres | 16 kB |
public | usr | table | postgres | 5008 kB |
schema1 | t1 | table | postgres | 0 bytes |
(10 rows)
Run Code Online (Sandbox Code Playgroud)
小智 34
从 pg_Admin 您可以简单地在当前数据库上运行以下命令,它将获取指定模式的所有表:
SELECT *
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema = 'public'
ORDER BY table_type, table_name
Run Code Online (Sandbox Code Playgroud)
这将为您提供所有永久表的列表(通常是您要查找的表)。如果将*通配符更改为table_name. 公共table_schema是大多数数据库的默认架构,除非您的管理员设置了新架构。
Joh*_*ell 19
可能您已将表插入到不在您的搜索路径中的模式中,或者默认模式,即 public 中,因此这些表不会使用 \dt 显示。如果您使用称为数据的模式,则可以通过运行来解决此问题,
alter database <databasename> set search_path=data, public;
退出并重新输入 psql,现在 \dt 也会显示模式数据中的表。
我很惊讶之前没有人提到过这一点,但是这些命令(来自此处的手册):
-E
--echo-hidden
Echo the actual queries generated by \d and other backslash commands. You can
use this to study psql's internal operations. This is equivalent to setting the
variable ECHO_HIDDEN to on.
Run Code Online (Sandbox Code Playgroud)
命令是学习 PostgreSQL 命令内部结构的好方法!
运行psql -E <db_name>然后运行(例如):
\dt
Run Code Online (Sandbox Code Playgroud)
然后输出是:
********* QUERY **********
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 't' THEN 'TOAST table' 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
LEFT JOIN pg_catalog.pg_am am ON am.oid = c.relam
WHERE c.relkind IN ('r','p','')
AND n.nspname <> 'pg_catalog'
AND n.nspname !~ '^pg_toast'
AND n.nspname <> 'information_schema'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
List of relations
Schema | Name | Type | Owner
--------+------------+-------+-------
public | int1test_1 | table | pol
public | int1test_2 | table | pol
public | x | table | pol
(3 rows)
Run Code Online (Sandbox Code Playgroud)
所以,该-E标志确实可以让您深入了解 PostgreSQL 的内部结构!
最后,另外,如果您已经启动了 psql 客户端(并且您不想退出并使用 -E 重新启动它),则可以使用\set ECHO_HIDDEN将标志设置为 on。
另外,再次从上面引用的手册dry run中,您可以通过将值设置为noexec:
如果将此变量设置为值 noexec,则只会显示查询,但不会实际发送到服务器并执行。默认值是关闭。
| 归档时间: |
|
| 查看次数: |
2763224 次 |
| 最近记录: |