postgresql客戶端命令之createdb
創建一個新的PostgreSQL數據庫。該命令的使用方式如下:
createdb [option...] [dbname] [description]
1. 命令行選項列表:
選項 | 說明 |
-D(--tablespace=tablespace) | 指定數據庫的缺省表空間。 |
-e(--echo) | 回顯createdb生成的命令並且把它發送到服務器。 |
-E(--encoding=encoding) | 指定用於此數據庫的字符編碼方式。 |
-l(--locale=locale) | 指定用於此數據庫的本地化設置。 |
-O(--owner=owner) | 指定新建數據庫的擁有者,如果未指定此選項,該值為當前登錄的用戶。 |
-T(--template=template | 指定創建此數據庫的模板數據庫。 |
-h(--host=host) | 指定PostgreSQL服務器的主機名。 |
-p(--port=port) | 指定服務器的偵聽端口,如不指定,則為缺省的5432。 |
-U(--username=username) | 本次操作的登錄用戶名,如果-O選項沒有指定,此數據庫的Owner將為該登錄用戶。 |
-w(--no-password) | 如果當前登錄用戶沒有密碼,可以指定該選項直接登錄。 |
2.應用示例:
#1.以postgres的身份登錄。
登陸默認的postgres數據庫(三種登陸方式)
[[email protected] PG_9.5_201510051]$ psql -p 36985
psql.bin (9.5.9)
Type "help" for help.
postgres=#
[[email protected] PG_9.5_201510051]$ psql -U postgres -p 36985
psql.bin (9.5.9)
Type "help" for help.
postgres=#
[[email protected] PG_9.5_201510051]$ psql -U postgres -d postgres -p 36985
psql.bin (9.5.9)
Type "help" for help.
postgres=# \q
#2. 創建表空間。
postgres=# CREATE TABLESPACE tbspace01 LOCATION ‘/data/postgresql/tbspace‘;
CREATE TABLESPACE
[[email protected] tbspace]$ cd /data/postgresql/tbspace
[[email protected] tbspace]$ ls
PG_9.5_201510051
3. 創建新數據庫的owner。
postgres=# CREATE ROLE testwjw LOGIN PASSWORD ‘123456‘;
CREATE ROLE
postgres=# \q
#4. 創建新數據庫,其中本次連接的登錄用戶為testwjw,新數據庫的owner為testwjw,新數據庫名為cstest01。
[[email protected] ~]$ createdb -U testwjw -p36985 -O testwjw -e cstest01
CREATE DATABASE cstest01 OWNER testwjw TABLESPACE db_space01;
createdb: database creation failed: ERROR: permission denied to create database
原因是用戶testwjw沒有創建庫的權限:
postgres=# alter user testwjw createdb;
ALTER ROLE
postgres=# \du List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
myuser | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
testwjw | Create DB
5.重新登錄,通過查詢系統表查看該數據庫是否創建成功,以及表空間和所有者是否一致。
postgres=# SELECT datname,rolname,spcname FROM pg_database db, pg_authid au, pg_tablespace ts WHERE datname = ‘cstest01‘ AND datdba = au.oid AND dattablespace = ts.oid;
datname | rolname | spcname
----------+---------+------------
cstest01 | testwjw | pg_default
(1 row)
本文出自 “10931853” 博客,請務必保留此出處http://wujianwei.blog.51cto.com/10931853/1970757
postgresql客戶端命令之createdb