官术网_书友最值得收藏!

User databases

You can have as many databases as you want in a database cluster. A client that connects to the PostgreSQL server can access only the data in a single database, which is specified in the connection string. That means data isn't shared between the databases unless the PostgreSQL foreign data wrapper or DB link extensions are used.

It's recommended to use PostgreSQL foreign data wrapper to do cross-database querying. DB link, if not used carefully, might cause memory  exhaustion issues.

Every database in the database cluster has an owner and a set of associated permissions to control the actions allowed for a particular role. The privileges on PostgreSQL objects, which include databases, views, tables, and sequences, are represented in the psql client as follows:

<user>=<privileges>/granted by

If the user part of the privileges isn't present, this means that the privileges are applied to PostgreSQL's special PUBLIC role.

The psql client tool's \l meta-command is used to list all the databases in the database cluster with the associated attributes:

postgres=# \l 
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------------+----------------+----------+-------------+-------------+-----------------------
car_portal | car_portal_app | 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

The database-access privileges are the following:

  • CREATE (-C): The create access privilege allows the specified role to create new schemas in the database.
  • CONNECT (-c): When a role tries to connect to a database, the connect permissions are checked.
  • TEMPORARY (-T): The temporary access privilege allows the specified role to create temporary tables. Temporary tables are very similar to tables, but they aren't persistent, and they're destroyed after the user session is terminated.

In the preceding example, the postgres database has no explicit privileges assigned. Also notice that the PUBLIC role is allowed to connect to the template1 database by default.

Encoding allows you to store text in a variety of character sets, including one-byte character sets, such as SQL_ASCII, or multiple-byte characters sets, such as UTF-8. PostgreSQL supports a rich set of character encodings. For the full list of character encodings, visit http://www.postgresql.org/docs/current/static/multibyte.html.

In addition to these attributes, PostgreSQL has several other attributes for various purposes, including the following:

  • Maintenance: The datfrozenxid attribute is used by autovaccum operations.
  • Storage management: The dattablespace attribute is used to determine to which tablespace the database belongs.
  • Concurrency: The datconnlimit attribute is used to determine the number of concurrent connections (-1 means no limits).
  • Protection: The datallowconn attribute disables the connection to a database. This is used mainly to protect template0 from being altered.
The psql client tool's  \c meta-command establishes a new connection to a database and closes the current one. It also accepts a connection string, such as a username and password.
postgres=# \c template0
FATAL: database "template0" is not currently accepting connections
Previous connection kept
pg_catalog tables are regular tables, thus you can use the SELECT, UPDATE, and DELETE operations to manipulate them. Doing so is not recommended, and needs the utmost attention. Manipulating catalog tables manually can lead  to silent errors, database crashes, data integrity corruption, and unexpected behavior.

The catalog tables are very useful for automating some tasks; Chapter 12, The PostgreSQL Catalog, is dedicated to pg_catalog. The following example shows how you can alter the connection to limit the database property by using the ALTER database command. The following example changes the datconnlimit value from -1 to 1:

postgres=# SELECT datconnlimit FROM pg_database WHERE datname='postgres'; 
datconnlimit
--------------
-1
(1 row)

postgres=# ALTER DATABASE postgres CONNECTION LIMIT 1;
ALTER DATABASE
主站蜘蛛池模板: 阿拉尔市| 平谷区| 茂名市| 阳东县| 松原市| 七台河市| 鄂伦春自治旗| 库伦旗| 电白县| 大城县| 葫芦岛市| 乌兰察布市| 澄城县| 尤溪县| 博白县| 嘉定区| 大新县| 东兰县| 扎囊县| 仲巴县| 屏南县| 大连市| 嘉鱼县| 定兴县| 锡林浩特市| 志丹县| 五原县| 杭锦后旗| 调兵山市| 周宁县| 江永县| 北流市| 西林县| 上饶市| 衡阳县| 横峰县| 怀化市| 乌兰浩特市| 平定县| 桃园市| 留坝县|