微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL数据库、表空间、角色及用户



分类MysqL/postgresql

一、创建数据库
1、通过pgAdmin创建数据库TestDb1:
打开数据库TestDb1看到建库脚本:


在目录——Postgresql(pg_catalog)——数据表——pg_database中可以查看多了一个数据库TestDb1:
select oid,* from pg_database;

此时,在E:\Postgresql\data\base下的文件夹如下:


以上说明,创建的数据库使用认表空间(pg_default),数据文件夹使用oid命名。
同时,在E:\Postgresql\data\local下也会增加一些文件
注:Postgresql的oid:行的对象表示符(对象ID);Postgresql的xid:事务ID;
这两个ID都是4字节的整数,在超过40亿时将溢出,此后会出现重复,所以,假设它们唯一是错误的,除非你自己采取了措施来保证它们是唯一的。

2、通过psql命令行创建数据库
postgres=# create database "TestDb2";
CREATE DATABASE
postgres=#
postgres=# select oid,datname from pg_database;
oid | datname
-------+-----------
1 | template1
11905 | template0
11913 | postgres
16393 | TestDb1
16394 | TestDb2
(5 行记录)
从pgAdmin打开,两者建库脚本一样,说明都是使用的认参数;

二、创建表空间
1、通过pgAdmin创建表空间
创建表空间必须首先建空目录,假设建文件夹:E:\Postgresql\data\TestDbs1,建表空间是选择该目录。
打开表空间TestDbs1,可以看到建表空间脚本:


2、通过psql命令行创建表空间
postgres=# create tablespace "TestDbs2" location 'E:\Postgresql\data\TestDbs2';
CREATE TABLESPACE

三、创建角色、用户
角色与用户的区别:
角色就相当于岗位:角色可以是经理,助理。
用户就是具体的人:比如陈XX经理,朱XX助理,王XX助理。
1、通过pgAdmin创建角色
创建角色TestRole1,输入密码,分配权限,需要分配login权限系统才会自动创建同名用户TestRole1。
打开角色TestRole1,可以看到创建角色脚本:


2、通过psql命令行创建角色
postgres=# create role "TestRole2";
CREATE ROLE
postgres=# select rolname from pg_roles;
rolname
-----------
postgres
TestRole1
TestRole2
(3 行记录)
postgres=# select usename from pg_user;
usename
-----------
postgres
TestRole1
(2 行记录)
postgres=# create role "TestRole3" login;
CREATE ROLE
postgres=# select rolname from pg_roles;
rolname
-----------
postgres
TestRole1
TestRole2
TestRole3
(4 行记录)
postgres=# select usename from pg_user;
usename
-----------
postgres
TestRole1
TestRole3
(3 行记录)
角色TestRole2创建时没有分配login权限,所以没有创建用户;在pgAdmin的“登录角色”中看不到,到pg_authid中把rolcanlogin字段由false改为true,系统自动创建同名用户,此时在“登录角色”中才可以看到。
也可以通过sql命令给该角色分配login权限,系统将自动创建同名用户TestRole2:
postgres=# select usename from pg_user;
usename
-----------
postgres
TestRole1
TestRole3
TestRole2
(4 行记录)
TestRole3角色创建时分配了login权限,系统自动创建一个同名的用户TestRole3。

通过psql命令行创建用户 Postgresql的create role与create user命令是等效的,只是前者认是非login的,而后者认是login的。 postgres=# create user "TestUser11" with password 'TestUser11' login in role "TestRole1"; CREATE ROLE postgres=# select rolname from pg_roles; rolname ------------ postgres TestRole1 TestRole3 TestRole2 TestUser11 (5 行记录) postgres=# select usename from pg_user; usename ------------ postgres TestRole1 TestRole3 TestRole2 TestUser11 (5 行记录) postgres=# create user "TestUser1"; CREATE ROLE postgres=# select rolname from pg_roles; rolname ------------ postgres TestRole1 TestRole3 TestRole2 TestUser11 TestUser1 (6 行记录) postgres=# select usename from pg_user; usename ------------ postgres TestRole1 TestRole3 TestRole2 TestUser11 TestUser1 (6 行记录)

原文地址:https://www.jb51.cc/postgresql/195304.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐