定义数据库对象
创建与管理数据库
通过\h命令查看创建数据库的语法,如下所示:
testdw-# \h create database Command: CREATE DATABASE Description: create a new database Syntax: CREATE DATABASE name [ [ WITH ] [ OWNER [=] dbowner ] [ TEMPLATE [=] template ] [ ENCODING [=] encoding ] [ LC_COLLATE [=] lc_collate ] [ LC_CTYPE [=] lc_ctype ] [ TABLESPACE [=] tablespace ] [ CONNECTION LIMIT [=] connlimit ] ] 连接限制 |
说明:
- 一个GPDB系统可以有多个数据库;
- 关于数据库模版:可以基于模版创建数据库,缺省数据库模版为template1,GP系统内部使用模板:template0和postgres;
- 创建数据库应该具备CREATEDB权限或者SUPERUSER身份;
- 通过CREATEDATABASE 命令创建;
template1=# create database devdw; CREATE DATABASE |
- 克隆一个数据库
template1=#\c devdw 使用\c连接数据库 You are now connected to database "devdw" as user "gpadmin". devdw=# create table tab_01(id int); 创建表 NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table. HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. CREATE TABLE devdw=# \d 使用\d查看表信息 List of relations Schema | Name | Type | Owner | Storage --------+--------+-------+---------+--------- public | tab_01 | table | gpadmin | heap (1 row) devdw=# insert into tab_01 values(101); 向表中插入数据 INSERT 0 1 devdw=# select * from tab_01; 查询表中信息 id ----- 101 (1 row) template1=# create database col_devdw template devdw; 使用devdw作为模板创建克隆数据库 CREATE DATABASE template1=# \l 使用\l命令查看当前所有数据库 List of databases Name | Owner | Encoding | Access privileges -----------+---------+----------+--------------------- col_devdw | gpadmin | UTF8 | devdw | gpadmin | UTF8 | postgres | gpadmin | UTF8 | template0 | gpadmin | UTF8 | =c/gpadmin : gpadmin=CTc/gpadmin template1 | gpadmin | UTF8 | =c/gpadmin : gpadmin=CTc/gpadmin testdw | gpadmin | UTF8 | (6 rows)
template1=# \c col_devdw You are now connected to database "col_devdw" as user "gpadmin". col_devdw=# \l List of databases Name | Owner | Encoding | Access privileges -----------+---------+----------+--------------------- col_devdw | gpadmin | UTF8 | devdw | gpadmin | UTF8 | postgres | gpadmin | UTF8 | template0 | gpadmin | UTF8 | =c/gpadmin : gpadmin=CTc/gpadmin template1 | gpadmin | UTF8 | =c/gpadmin : gpadmin=CTc/gpadmin testdw | gpadmin | UTF8 | (6 rows)
col_devdw=# \d List of relations Schema | Name | Type | Owner | Storage --------+--------+-------+---------+--------- public | tab_01 | table | gpadmin | heap (1 row) |
- 查看数据库列表:可以通过查询pg_database系统日志表
template1=# \l List of databases Name | Owner | Encoding | Access privileges -----------+---------+----------+--------------------- devdw | gpadmin | UTF8 | gpadmin | gpadmin | UTF8 | postgres | gpadmin | UTF8 | template0 | gpadmin | UTF8 | =c/gpadmin : gpadmin=CTc/gpadmin template1 | gpadmin | UTF8 | =c/gpadmin : gpadmin=CTc/gpadmin testdw | gpadmin | UTF8 | (6 rows) template1=# SELECT * FROM pg_database; datname | datdba | encoding | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | dattablespace | datconfig | datacl
-----------+--------+----------+---------------+--------------+--------------+---------------+--------------+---------------+-----------+---------------------------------- testdw | 10 | 6 | f | t | -1 | 10899 | 803 | 1663 | | postgres | 10 | 6 | t | t | -1 | 10899 | 803 | 1663 | | devdw | 10 | 6 | f | t | -1 | 10899 | 803 | 1663 | | template1 | 10 | 6 | t | t | -1 | 10899 | 803 | 1663 | | {=c/gpadmin,gpadmin=CTc/g padmin} template0 | 10 | 6 | t | f | -1 | 10899 | 803 | 1663 | | {=c/gpadmin,gpadmin=CTc/g padmin} (5 rows) |
- 变更数据库:必须具备Owner或者SUPERUSER权限且使用ALTERDATABASE命令来改变DB的属性
template1=# \h ALTER DATABASE Command: ALTER DATABASE Description: change a database Syntax: ALTER DATABASE name [ [ WITH ] option [ ... ] ]
where option can be: CONNECTION LIMIT connlimit ALTER DATABASE name SET parameter { TO | = } { value | DEFAULT } ALTER DATABASE name RESET parameter ALTER DATABASE name RENAME TO newname ALTER DATABASE name OWNER TO new_owner |
template1=# alter database devdw set search_path to public,pg_catalog; ALTER DATABASE template1=# select * from pg_database; datname | datdba | encoding | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | dattablespace | datconfig | datacl -----------+--------+----------+---------------+--------------+--------------+---------------+--------------+---------------+------------------------------------+---------------------------------- testdw | 10 | 6 | f | t | -1 | 10899 | 803 | 1663 | | postgres | 10 | 6 | t | t | -1 | 10899 | 803 | 1663 | | col_devdw | 10 | 6 | f | t | -1 | 10899 | 803 | 1663 | | template1 | 10 | 6 | t | t | -1 | 10899 | 803 | 1663 | | {=c/gpadmin,gpadmin=CTc/gpadmin} template0 | 10 | 6 | t | f | -1 | 10899 | 803 | 1663 | | {=c/gpadmin,gpadmin=CTc/gpadmin} devdw | 10 | 6 | f | t | -1 | 10899 | 803 | 1663 | {"search_path=public, pg_catalog"} | (6 rows) |
- 删除数据库:必须具备Owner或者SUPERUSER权限,通过CREATEDATABASE 命令删除
testdw-# \h drop database Command: DROP DATABASE Description: remove a database Syntax: DROP DATABASE [ IF EXISTS ] name |
template1=# drop database col_devdw; DROP DATABASE |
来源:oschina
链接:https://my.oschina.net/u/4370811/blog/4486350