(八)PostgreSQL的数据库管理

PostgreSQL的数据库管理

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

1 创建数据库

CREATE DATABASE创建一个新的PostgreSQL数据库。 
若要创建数据库,您必须是超级用户或具有特殊的CREATEDB权限。请参阅CREATE ROLE部分内容。 
默认情况下,将通过克隆系统数据库template1来创建新数据库。也可以通过参数 template 来指定不同的模板。特别是,通过指定TEMPLATE template0,可以创建一个原始数据库(其中不存在用户定义的对象,并且系统对象没有更改),其中只包含PostgreSQL版本预定义的标准对象。这样可以避免复制任何可能已添加到template1中的本地对象。

语法

CREATE DATABASE name
[ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ STRATEGY [=] strategy ] ]
[ LOCALE [=] locale ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ ICU_LOCALE [=] icu_locale ]
[ ICU_RULES [=] icu_rules ]
[ LOCALE_PROVIDER [=] locale_provider ]
[ COLLATION_VERSION = collation_version ]
[ TABLESPACE [=] tablespace_name ]
[ ALLOW_CONNECTIONS [=] allowconn ]
[ CONNECTION LIMIT [=] connlimit ]
[ IS_TEMPLATE [=] istemplate ]
[ OID [=] oid ]

参数解析

  • name
    The name of a database to create.
    要创建的数据库的名称。

  • user_name
    The role name of the user who will own the new database, or DEFAULT to use the default (namely, the user executing the command). To create a database owned by another role, you must be able to SET ROLE to that role.
    将拥有新数据库的用户的角色名称,或使用默认值的DEFAULT(即执行命令的用户)。若要创建由另一个角色拥有的数据库,您必须能够将角色设置为该角色。

  • template
    The name of the template from which to create the new database, or DEFAULT to use the default template (template1).
    用于创建新数据库的模板的名称,或使用默认模板 template1。

  • encoding
    Character set encoding to use in the new database. Specify a string constant (e.g.,
    ‘SQL_ASCII’), or an integer encoding number, or DEFAULT to use the default encoding (namely, the encoding of the template database). The character sets supported by the PostgreSQL server
    are described in Section 24.3.1. See below for additional restrictions.
    要在新数据库中使用的字符集编码。指定字符串常量(例如“SQL_ASCII”)、整数编码数字或使用默认编码(即模板数据库的编码)。PostgreSQL服务器支持的字符集如第24.3.1节所述。有关其他限制,请参见下文。

  • strategy
    Strategy to be used in creating the new database. If the WAL_LOG strategy is used, the database
    will be copied block by block and each block will be separately written to the write-ahead log.
    This is the most efficient strategy in cases where the template database is small, and therefore it is
    the default. The older FILE_COPY strategy is also available. This strategy writes a small record
    to the write-ahead log for each tablespace used by the target database. Each such record represents copying an entire directory to a new location at the filesystem level. While this does reduce the write-ahead log volume substantially, especially if the template database is large, it also forces the system to perform a checkpoint both before and after the creation of the new database. In some situations, this may have a noticeable negative impact on overall system performance.
    用于创建新数据库的策略。如果使用WAL_LOG策略,数据库将被逐块复制,并且每个块将分别写入预写日志。在模板数据库很小的情况下,这是最有效的策略,因此它是默认的。旧的FILE_COPY策略也可用。此策略为目标数据库使用的每个表空间向预写日志中写入一条小记录。每个这样的记录都表示将整个目录复制到文件系统级别的新位置。虽然这确实大大减少了预写日志量,特别是在模板数据库很大的情况下,但它也迫使系统在创建新数据库之前和之后执行检查点。在某些情况下,这可能会对整个系统性能产生明显的负面影响。

  • locale
    Sets the default collation order and character classification in the new database. Collation affects the sort order applied to strings, e.g., in queries with ORDER BY, as well as the order used in indexes on text columns. Character classification affects the categorization of characters, e.g., lower, upper, and digit. Also sets the associated aspects of the operating system environment, LC_COLLATE and LC_CTYPE. The default is the same setting as the template database. See Section 24.2.2.3.1 and Section 24.2.2.3.2 for details.
    设置新数据库中的默认排序规则顺序和字符分类。排序规则影响应用于字符串的排序顺序,例如,在使用order BY的查询中,以及在文本列的索引中使用的顺序。字符分类会影响字符的分类,例如低位、高位和数字。还设置操作系统环境的相关方面LC_COLLATE和LC_CTYPE。默认设置与模板数据库的设置相同。详见第24.2.2.3.1节和第24.2.2.3.2节。

  • lc_collate
    Sets LC_COLLATE in the database server’s operating system environment. The default is the
    setting of locale if specified, otherwise the same setting as the template database. See below
    for additional restrictions.
    If locale_provider is libc, also sets the default collation order to use in the new database,
    overriding the setting locale.
    在数据库服务器的操作系统环境中设置LC_COLLATE。默认值是区域设置(如果指定),否则与模板数据库的设置相同。有关其他限制,请参见下文。
    如果locale_provider是libc,还将设置要在新数据库中使用的默认排序规则顺序,覆盖设置区域设置。

  • lc_ctype
    Sets LC_CTYPE in the database server’s operating system environment. The default is the setting of locale if specified, otherwise the same setting as the template database. See below for additional restrictions.
    If locale_provider is libc, also sets the default character classification to use in the new database, overriding the setting locale.
    在数据库服务器的操作系统环境中设置LC_CTYPE。默认值是区域设置(如果指定),否则与模板数据库的设置相同。有关其他限制,请参见下文。
    如果locale_provider是libc,还将设置要在新数据库中使用的默认字符分类,从而覆盖设置区域设置。

  • icu_locale
    Specifies the ICU locale (see Section 24.2.2.3.2) for the database default collation order and character classification, overriding the setting locale. The locale provider must be ICU. The default is the setting of locale if specified; otherwise the same setting as the template database.
    为数据库默认排序规则顺序和字符分类指定ICU区域设置(请参见第24.2.2.3.2节),覆盖设置区域设置。区域设置提供程序必须是ICU。如果指定,则默认为区域设置;否则设置与模板数据库相同

  • icu_rules
    Specifies additional collation rules to customize the behavior of the default collation of this database. This is supported for ICU only. See Section 24.2.3.4 for details.
    指定其他排序规则以自定义此数据库的默认排序规则的行为。这仅适用于ICU。详见第24.2.3.4节。

  • locale_provider
    Specifies the provider to use for the default collation in this database. Possible values are icu (if the server was built with ICU support) or libc. By default, the provider is the same as that of
    the template. See Section 24.1.4 for details.
    指定用于此数据库中默认排序规则的提供程序。可能的值是icu(如果服务器是在icu支持下构建的)或libc。默认情况下,提供程序与模板的提供程序相同。详见第24.1.4节。

  • collation_version
    Specifies the collation version string to store with the database. Normally, this should be omitted, which will cause the version to be computed from the actual version of the database collation as provided by the operating system. This option is intended to be used by pg_upgrade for copying the version from an existing installation.
    See also ALTER DATABASE for how to handle database collation version mismatches.
    指定要与数据库一起存储的排序规则版本字符串。通常情况下,应该忽略这一点,这将导致根据操作系统提供的数据库排序规则的实际版本来计算版本。此选项用于pg_upgrade从现有安装中复制版本。
    有关如何处理数据库排序规则版本不匹配的信息,请参阅ALTER DATABASE。

  • tablespace_name
    The name of the tablespace that will be associated with the new database, or DEFAULT to use
    the template database’s tablespace. This tablespace will be the default tablespace used for objects created in this database. See CREATE TABLESPACE for more information.
    将与新数据库相关联的表空间的名称,或默认使用模板数据库表空间。此表空间将是用于在此数据库中创建的对象的默认表空间。有关详细信息,请参见CREATE TABLESPACE。

  • allowconn
    If false then no one can connect to this database. The default is true, allowing connections (except as restricted by other mechanisms, such as GRANT/REVOKE CONNECT).
    如果为false,则没有人可以连接到此数据库。默认值为true,允许连接(除非受到其他机制的限制,如GRANT/REVOKE CONNECT)。

  • connlimit
    How many concurrent connections can be made to this database. -1 (the default) means no limit.
    可以与此数据库建立多少个并发连接-1(默认值)表示没有限制。

  • istemplate
    If true, then this database can be cloned by any user with CREATEDB privileges; if false (the
    default), then only superusers or the owner of the database can clone it.
    如果为true,则任何具有CREATEDB权限的用户都可以克隆此数据库;如果为false(默认值),则只有超级用户或数据库所有者才能克隆它。

  • oid
    The object identifier to be used for the new database. If this parameter is not specified, PostgreSQL will choose a suitable OID automatically. This parameter is primarily intended for internal use by pg_upgrade, and only pg_upgrade can specify a value less than 16384.
    要用于新数据库的对象标识符。如果没有指定这个参数,PostgreSQL会自动选择一个合适的OID。此参数主要供pg_upgrade内部使用,只有pg_upggrade才能指定小于16384的值。

Optional parameters can be written in any order, not only the order illustrated above.
以上可选参数可以按照任何顺序写入,而不仅仅是按照上面所示的顺序。

case 1:创建数据库test1

postgres=# create database test1;
CREATE DATABASE
postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 test1     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(5 rows)

case 2:创建数据库test2,并指定表空间。

--创建表空间test1
mkdir -p /pgdir/test1
chown -R pg16:pg16  /pgdir/test1

[pg16@test test1]$ psql -p 5777
psql (16.2)
Type "help" for help.

postgres=# CREATE TABLESPACE test1 LOCATION '/pgdir/test1';
CREATE TABLESPACE
postgres=# \db
         List of tablespaces
    Name    |  Owner   |   Location   
------------+----------+--------------
 pg_default | postgres | 
 pg_global  | postgres | 
 test1      | postgres | /pgdir/test1
(3 rows)

–创建数据库test2

postgres=# create database test2 tablespace test1;
CREATE DATABASE
postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 test1     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 test2     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(6 rows)

postgres=# select b.datname,a.spcname from pg_tablespace a,pg_database b where a.oid=b.dattablespace and b.datname='test2';
 datname | spcname 
---------+---------
 test2   | test1
(1 row)

2 修改数据库

语法

ALTER DATABASE name [ [ WITH ] option [ ... ] ]
where option can be:
ALLOW_CONNECTIONS allowconn
CONNECTION LIMIT connlimit
IS_TEMPLATE istemplate
ALTER DATABASE name RENAME TO new_name
ALTER DATABASE name OWNER TO { new_owner | CURRENT_ROLE |
CURRENT_USER | SESSION_USER }
ALTER DATABASE name SET TABLESPACE new_tablespace
ALTER DATABASE name REFRESH COLLATION VERSION
ALTER DATABASE name SET configuration_parameter { TO | = } { value
| DEFAULT }
ALTER DATABASE name SET configuration_parameter FROM CURRENT
ALTER DATABASE name RESET configuration_parameter
ALTER DATABASE name RESET ALL

参数解析

  • name
    The name of the database whose attributes are to be altered.

  • allowconn
    If false then no one can connect to this database.

  • connlimit
    How many concurrent connections can be made to this database. -1 means no limit.

  • istemplate
    If true, then this database can be cloned by any user with CREATEDB privileges; if false, then
    only superusers or the owner of the database can clone it.

  • new_name
    The new name of the database.

  • new_owner
    The new owner of the database.

  • new_tablespace
    The new default tablespace of the database.
    This form of the command cannot be executed inside a transaction block.

  • REFRESH COLLATION VERSION
    Update the database collation version. See Notes for background.

  • configuration_parameter

  • value
    Set this database’s session default for the specified configuration parameter to the given value. Ifvalue is DEFAULT or, equivalently, RESET is used, the database-specific setting is removed,
    so the system-wide default setting will be inherited in new sessions. Use RESET ALL to clear
    all database-specific settings. SET FROM CURRENT saves the session’s current value of the
    parameter as the database-specific value.

case 1:数据库test1的表空间为test1

postgres=# select b.datname,a.spcname from pg_tablespace a,pg_database b where a.oid=b.dattablespace and b.datname='test1';
 datname |  spcname   
---------+------------
 test1   | pg_default
(1 row)

postgres=# alter database test1 set tablespace test1;
ALTER DATABASE
postgres=# select b.datname,a.spcname from pg_tablespace a,pg_database b where a.oid=b.dattablespace and b.datname='test1';
 datname | spcname 
---------+---------
 test1   | test1
(1 row)

postgres=# 

case 2:修改数据库test1的名字为test1_new

postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 test1     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 test2     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(6 rows)

postgres=# alter database test1 rename to test1_new;
ALTER DATABASE
postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 test1_new | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 test2     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(6 rows)

3 删除数据库

DROP DATABASE删除数据库。它删除数据库的目录条目,并删除包含数据的目录。它只能由数据库所有者执行。当您连接到目标数据库时,它无法执行。(连接到postgres或任何其他数据库以发出此命令。)此外,如果其他任何人连接到目标数据库,则除非使用下面描述的FORCE选项,否则此命令将失败。
DROP DATABASE无法撤消。小心使用!

语法

DROP DATABASE [ IF EXISTS ] name [ [ WITH ] ( option [, ...] ) ]
where option can be:
FORCE

case 1:删除数据库test1

postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 test1     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 test2     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(6 rows)

postgres=# drop database test1;
DROP DATABASE
postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 test2     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(5 rows)

case 2:删除数据库test2,加force参数。

postgres=# drop database test2;
ERROR:  database "test2" is being accessed by other users
DETAIL:  There is 1 other session using the database.
                       ^
postgres=# drop database test2 with (force);
DROP DATABASE
postgres=# 
postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(4 rows)

postgres=# 

更多内容请查看对应数据库版本的官方文档。

谨记:心存敬畏,行有所止。

  • 25
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值