How can I show the catalogs and schemas in postgresql?
来自https://stackoverflow.com/a/17943883/156458
in both Postgres and the SQL Standard we have this containment
hierarchy:
- A computer may have one cluster or multiple.
- A database server is a cluster.
- A cluster has [catalogs][8]. ( Catalog = Database )
- Catalogs have [schemas][9]. (Schema = [namespace][10] of tables, and security boundary)
- Schemas have [tables][11].
- Tables have [rows][12].
- Rows have values, defined by [columns][13].
在postgresql中,我有一个名为
1 2 3 4 | postgres=# \c students You are now connected TO DATABASE"students" AS USER"postgres". students=# \dt; public | student | TABLE | postgres |
我想知道数据库
一般来说,我该如何列出
- 所有的目录和
- 目录下的所有模式,以及
- 架构下的所有表格?
我怎么能表现出来
- 当前目录,和
- 目前的架构?
谢谢。
很快,我通常会向我的初级队友解释他们开始研究PostgreSQL的问题。
在PostgreSQL中:
- 数据库服务器就像一个工业区,会有很多数据库 - 集群(建筑物)
- 每个数据库群集(建筑物)都有许多数据库。这些就像建筑物的地板。在您调用目录时,数据库就在这里。那些数据库 - 目录(楼层)相互独立,你不能直接使用其他材料,你必须使用像楼梯,电线......(在数据库中是DBLINK)。
- 好的,接下来,每个数据库目录都有许多模式,就像您楼层上的许多房间一样。这些架构可以使用彼此的材料。
- 然后,每个Schema都有许多单元格元素,如Table,View,Function,Sequence ....所有模式都具有相同的结构。
现在,回到你的例子:
- 学生:是数据库(你称之为目录)
- public:是架构。
- 学生:是桌子。
你可以列出:
-
目录(数据库)由psql中的命令
\l 或查询select * from pg_database; -
psql中的命令
\dn 或查询select * from information_schema.schemata; 下的目录下的模式 -
查询
select * from pg_tables WHERE schemaname = 'Your schema'; 下的模式下的表
你可以显示:
-
查询
select current_database(); 的当前数据库(目录) -
查询
select current_schema; 的当前架构
请注意PostgreSQL有两个系统架构调用information_schema和pg_catalog,这可能会让你感到困惑。
pg_catalog是一个系统架构。更多信息。
The system catalogs are the place where a relational database management system stores schema metadata, such as information about tables and columns, and internal bookkeeping information. PostgreSQL's system catalogs are regular tables. You can drop and recreate the tables, add columns, insert and update values, and severely mess up your system that way. Normally, one should not change the system catalogs by hand, there are always SQL commands to do that. (For example, CREATE DATABASE inserts a row into the pg_database catalog — and actually creates the database on disk.) There are some exceptions for particularly esoteric operations, such as adding index access methods.
information_schema是系统架构。更多信息。
The information schema consists of a set of views that contain information about the objects defined in the current database. The information schema is defined in the SQL standard and can therefore be expected to be portable and remain stable — unlike the system catalogs, which are specific to PostgreSQL and are modeled after implementation concerns. The information schema views do not, however, contain information about PostgreSQL-specific features; to inquire about those you need to query the system catalogs or other PostgreSQL-specific views.
我希望这些信息能够帮助你清楚。