數據庫管理
更新時間 2025-02-14 10:22:16
最近更新時間: 2025-02-14 10:22:16
分享文章
本文為您介紹創建、修改和刪除數據庫。
創建數據庫
要創建一個數據庫,必須是一個超級用戶或者具有特殊的CREATEDB特權,默認情況下,新數據庫將通過克隆標準系統數據庫template1被創建。可以通過寫TEMPLATE name指定一個不同的模板。特別地,通過寫TEMPLATE template0您可以創建一個干凈的數據庫,它將只包含的Teledb所預定義的標準對象。
默認參數創建數據庫
teledb=# create database teledb_db; CREATE DATABASE指定克隆庫
teledb=# create database teledb_db_template TEMPLATE template0; CREATE DATABASE指定所有者
teledb=# create role teledb_user with login; CREATE ROLE teledb=# create database teledb_db_owner owner teledb_user; CREATE DATABASE teledb=# \l+ teledb_db_owner List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description ------------------+--------------+----------+-------------+-------------+-------------------+-------+------------+------------- teledb_db_owner | teledb_user | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | 19 MB | pg_default | (1 row)指定編碼
teledb=# create database teledb_db_encoding ENCODING UTF8; CREATE DATABASE teledb=# \l+ teledb_db_encoding List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description ---------------------+---------+----------+-------------+-------------+-------------------+-------+------------+------------- teledb_db_encoding | teledb | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | | 19 MB | pg_default | (1 row)創建gbk編碼
teledb=# CREATE DATABASE db_gbk template template0 encoding = gbk LC_COLLATE = 'zh_CN.gbk' LC_CTYPE = 'zh_CN.gbk'; CREATE DATABASE teledb=# \d db_gbk Did not find any relation named "db_gbk". teledb=# \l+ db_gbk List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description --------+---------+----------+-----------+-----------+-------------------+-------+------------+------------- db_gbk | teledb | GBK | zh_CN.gbk | zh_CN.gbk | | 19 MB | pg_default | (1 row)指定排序規則
teledb=# create database teledb_db_lc_collate lc_collate 'C' template template0; CREATE DATABASE指定分組規則
teledb=# create database teledb_lc_ctype LC_CTYPE 'C' template template0; CREATE DATABASE配置數據可連接
teledb=# create database teledb__allow_connections ALLOW_CONNECTIONS true; CREATE DATABASE teledb=# select datallowconn from pg_database where datname = 'teledb__allow_connections'; datallowconn -------------- t (1 row) teledb=# \c teledb__allow_connections You are now connected to database "teledb__allow_connections" as user "teledb".配置連接數
teledb=# create database teledb_connlimit CONNECTION LIMIT 100; CREATE DATABASE teledb=# select datconnlimit from pg_database where datname='teledb_connlimit'; datconnlimit -------------- 100 (1 row)配置數據庫可以被復制(是否模板數據庫)
teledb=# create database teledb_istemplate is_template true; CREATE DATABASE teledb=# select datconnlimit from pg_database where datname='teledb_connlimit'; datconnlimit -------------- 100 (1 row)多個參數一起配置
teledb=# create database teledb_mul owner teledb_user CONNECTION LIMIT 50 template template0 encoding 'utf8' lc_collate 'C'; CREATE DATABASE修改數據庫配置
修改數據庫名稱
teledb=# alter database teledb_db rename to teledb_db_new; ALTER DATABASE修改連接數
teledb=# alter database teledb_db_new connection limit 50; ALTER DATABASE修改數據庫所有者
teledb=# alter database teledb_db_new owner to teledb; ALTER DATABASE配置數據默認search_path
teledb=# alter database teledb_db_new set search_path to public, pg_catalog; ALTER DATABASEalter database不支持修改的項目
項目 備注 encoding 編碼 lc_collate 排序規則 lc_ctype 分組規則
刪除數據庫
-- 刪除數據庫teledb_db_new
teledb=# drop database teledb_db_new;
-- 仍有會話連接數據庫時,會報錯
ERROR: database "teledb_db_new" is being accessed by other users
DETAIL: There is 1 other session using the database.
-- 停止該數據庫的所有連接,后重新刪除數據庫
teledb=# select pg_terminate_backend(pid) from pg_stat_activity where datname='teledb_db_new';
pg_terminate_backend
----------------------
t
(1 row)
teledb=# drop database teledb_db_new;
DROP DATABASE