[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=>
查看对应库的schemas
mydb=> \dn
List of schemas
Name | Owner
--------+----------
public | postgres
(1 row)
查看表空间和用户 \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)
创建schema
创建schema CREATE SCHEMA myschema;
创建表
CREATE TABLE "myschema"."o_ls_test" ( "rid" int4, "name" varchar(200) COLLATE "pg_catalog"."default", "timeflag" timestamp without time zone default clock_timestamp() ) ; ALTER TABLE "myschema"."o_ls_test" OWNER TO "pguser"; COMMENT ON COLUMN "myschema"."o_ls_test"."rid" IS 'rid'; COMMENT ON COLUMN "myschema"."o_ls_test"."name" IS '名称'; COMMENT ON COLUMN "myschema"."o_ls_test"."timeflag" IS '创建时间'; COMMENT ON TABLE "myschema"."o_ls_test" IS '测试表';
CREATE INDEX "rid_index" ON "myschema"."o_ls_test" USING btree (
"rid" ASC NULLS LAST
);
生成测试数据
insert into "myschema"."o_ls_test"( rid ,name ) select n ,n || '测试名字' || n from generate_series(1,5000000) n ;
查看搜索路径
mydb=> SHOW search_path; search_path ----------------- "$user", public (1 row) mydb=> SELECT current_schema(); current_schema ---------------- public (1 row) 指定搜索路径 mydb=> SET search_path TO public, myschema ; SET mydb=> SELECT current_schema(); current_schema ---------------- public (1 row) mydb=> SHOW search_path; search_path ------------------ public, myschema (1 row)
查看搜索路径下schema的所有表
mydb=> \dt List of relations Schema | Name | Type | Owner ----------+-----------+-------+-------- myschema | o_ls_test | table | pguser
postgres@[local]:5432=#alter system set search_path = "$user",public,myschema;
ALTER SYSTEM
postgres@[local]:5432=#show search_path;
search_path
-----------------
"$user", public
(1 row)
postgres@[local]:5432=#SELECT pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)
postgres@[local]:5432=#show search_path;
search_path
---------------------------
"$user", public, myschema
表空间 tablespace
数据库 database
schema schema