登录数据库 su postgres #注意这里postgers 前后都有空格 psql -U postgres -d postgres -p 5432
[centos@s101 /home/centos]$su postgres [postgres@s101 /home/centos]$cd / [postgres@s101 /]$psql -U postgres -d postgres -p 5432
用户创建
CREATE ROLE pguser WITH ENCRYPTED PASSWORD '123456';
postgres=# CREATE ROLE pguser WITH ENCRYPTED PASSWORD '123456';
CREATE ROLE
创建表空间目录
[postgres@s101 /var/lib/pgsql/13]$mkdir -p /var/lib/pgsql/13/pg_tbs/tbs_mydb
表空间创建
postgres=# create tablespace tbs_mydb owner pguser location '/var/lib/pgsql/13/pg_tbs/tbs_mydb';
CREATE TABLESPACE
数据库创建
CREATE DATABASE "mydb" WITH OWNER = "pguser" TEMPLATE = "template0" ENCODING = 'UTF8' TABLESPACE = "tbs_mydb" ; COMMENT ON DATABASE "mydb" IS '测试数据库';
赋予权限
postgres=# grant all on database mydb to pguser with grant option;
GRANT
postgres=# grant all on tablespace tbs_mydb to pguser ;
GRANT
需要给用户登录权限,不然会报错
postgres=# ALTER ROLE pguser LOGIN;
ALTER ROLE
测试登录
[postgres@s101 /]$psql -h localhost -p 5432 mydb pguser
Password for user pguser:
psql (9.2.24, server 13.3)
WARNING: psql version 9.2, server version 13.0.
Some psql features might not work.
Type "help" for help.
mydb=>
查看表空间和用户 \db
postgres=# \db
List of tablespaces
Name | Owner | Location
------------+----------+-----------------------------------
pg_default | postgres |
pg_global | postgres |
tbs_mydb | pguser | /var/lib/pgsql/13/pg_tbs/tbs_mydb
(3 rows)