加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql客户端命令之createdb

发布时间:2020-12-13 16:47:49 所属栏目:百科 来源:网络整理
导读:创建一个新的 PostgreSQL 数据库。该命令的使用方式如下: createdb [option...] [dbname] [description] 1. 命令行选项列表: 选项 说明 -D (--tablespace=tablespace) 指定数据库的缺省表空间。 -e (--echo) 回显 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数据库(三种登陆方式)

[postgres@localhost PG_9.5_201510051]$ psql -p 36985

psql.bin (9.5.9)

Type "help" for help.

postgres=#

[postgres@localhost PG_9.5_201510051]$ psql -U postgres -p 36985

postgres=#

[postgres@localhost PG_9.5_201510051]$ psql -U postgres -d postgres -p 36985

postgres=# q


#2. 创建表空间。

postgres=# CREATE TABLESPACE tbspace01 LOCATION '/data/postgresql/tbspace';

CREATE TABLESPACE

[postgres@localhost tbspace]$ cd /data/postgresql/tbspace

[postgres@localhost tbspace]$ ls

PG_9.5_201510051

3. 创建新数据库的ownerpostgres=# CREATE ROLE testwjw LOGIN PASSWORD '123456';
CREATE ROLE
postgres=# q

#4.创建新数据库,其中本次连接的登录用户为testwjw,新数据库的ownertestwjw,新数据库名为cstest01

[postgres@localhost ~]$ 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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读