Postgresql 中文操作指南
23.1. Overview #
少数对象(例如角色、数据库和表空间名称)在群集级别定义并在 pg_global 表空间中存储。群集内部有多个数据库,它们彼此隔离,但可以访问群集级对象。每个数据库内部有多个架构,其中包含表和函数等对象。因此,完整的层次结构为:群集、数据库、架构、表(或某种其他类型的对象,例如函数)。
A small number of objects, like role, database, and tablespace names, are defined at the cluster level and stored in the pg_global tablespace. Inside the cluster are multiple databases, which are isolated from each other but can access cluster-level objects. Inside each database are multiple schemas, which contain objects like tables and functions. So the full hierarchy is: cluster, database, schema, table (or some other kind of object, such as a function).
在连接到数据库服务器时,客户端必须在其连接请求中指定数据库名称。每连接不能访问多个数据库。但是,客户端可以打开与同一个数据库或者不同数据库的多个连接。数据库级别的安全性包含两个组成部分:在连接级别管理的访问控制(见 Section 21.1)和通过授予系统管理的授权控制(见 Section 5.7)。外部数据包装器(参见 postgres_fdw)允许一个数据库中的对象作为其他数据库或集群中对象的代理。较旧的dblink模块(参见 dblink)提供了相似的功能。在默认情况下,所有用户都使用所有连接方法连接所有数据库。
When connecting to the database server, a client must specify the database name in its connection request. It is not possible to access more than one database per connection. However, clients can open multiple connections to the same database, or different databases. Database-level security has two components: access control (see Section 21.1), managed at the connection level, and authorization control (see Section 5.7), managed via the grant system. Foreign data wrappers (see postgres_fdw) allow for objects within one database to act as proxies for objects in other database or clusters. The older dblink module (see dblink) provides a similar capability. By default, all users can connect to all databases using all connection methods.
如果计划一个PostgreSQL服务器集群包含不相关的项目或用户,而这些项目或用户在绝大多数情况下彼此不知道,则建议将它们放入单独的数据库中,并相应调整授权和访问控制。如果这些项目或用户是相互关联的,因此应当能够使用彼此的资源,则应当将它们放入同一个数据库中,但可能需要将它们放到单独的模式中;这提供了一个具备命名空间隔离和授权控制的模块化结构。在 Section 5.9中提供了有关如何管理模式的更多信息。
If one PostgreSQL server cluster is planned to contain unrelated projects or users that should be, for the most part, unaware of each other, it is recommended to put them into separate databases and adjust authorizations and access controls accordingly. If the projects or users are interrelated, and thus should be able to use each other’s resources, they should be put in the same database but probably into separate schemas; this provides a modular structure with namespace isolation and authorization control. More information about managing schemas is in Section 5.9.
尽管能够在单个集群中创建多个数据库,但建议您谨慎考虑此做法的优点是否大于风险和限制。具体来说,考虑共用WAL(参见 Chapter 30)对备份和恢复选项产生的影响。尽管从用户的角度考虑时,集群中的各个数据库是孤立的,但从数据库管理员的角度考虑时,它们紧密联系在一起。
While multiple databases can be created within a single cluster, it is advised to consider carefully whether the benefits outweigh the risks and limitations. In particular, the impact that having a shared WAL (see Chapter 30) has on backup and recovery options. While individual databases in the cluster are isolated when considered from the user’s perspective, they are closely bound from the database administrator’s point-of-view.
数据库可以通过_CREATE DATABASE_命令(见 Section 23.2)创建,可以通过_DROP DATABASE_命令(见 Section 23.5)删除。若要确定现有数据库的集合,请检查_pg_database_系统目录,如下所示:
Databases are created with the CREATE DATABASE command (see Section 23.2) and destroyed with the DROP DATABASE command (see Section 23.5). To determine the set of existing databases, examine the pg_database system catalog, for example
SELECT datname FROM pg_database;
psql 程序的 \l 元命令和 -l 命令行选项对于列出现有数据库也很有用。
The psql program’s \l meta-command and -l command-line option are also useful for listing the existing databases.