first commit
This commit is contained in:
commit
ba848e218d
1001 changed files with 152333 additions and 0 deletions
BIN
数据库/postgresql/PostgreSQL CPU占用100%性能分析及慢sql优化.docx
Normal file
BIN
数据库/postgresql/PostgreSQL CPU占用100%性能分析及慢sql优化.docx
Normal file
Binary file not shown.
Binary file not shown.
25
数据库/postgresql/monitor.sh
Normal file
25
数据库/postgresql/monitor.sh
Normal file
|
@ -0,0 +1,25 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Edit 2020/04/16
|
||||
|
||||
# sql script
|
||||
# https://blog.csdn.net/Hehuyi_In/article/details/95893869
|
||||
# example:
|
||||
# sql="select count(*) from pg_stat_activity where state='idle in transaction';"
|
||||
# message="处于空闲状态的会话"
|
||||
sql=""
|
||||
message=""
|
||||
|
||||
user=postgres
|
||||
source /home/$user/.bash_profile
|
||||
|
||||
|
||||
if [ $UID -ne `id -u $user` ];then
|
||||
echo -e "\033[31;1mNot Manager of PostgreSQL! \033[0m"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# get value
|
||||
valuse=`psql postgres -c "$sql" | sed -n '3p' | sed 's/ //g'`
|
||||
|
||||
echo "$message=$valuse"
|
27
数据库/postgresql/postgres-backup.sh
Normal file
27
数据库/postgresql/postgres-backup.sh
Normal file
|
@ -0,0 +1,27 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Edit 2020/4/10
|
||||
|
||||
backup=$PGDATA/../backup
|
||||
now=`date +%Y%m%d-%H%M%S`
|
||||
aweekago=`date -d -7days +%Y%m%d`
|
||||
|
||||
host=10.1.139.126
|
||||
port=5432
|
||||
username=yanzhao
|
||||
database=yzdb
|
||||
|
||||
if [ $UID -eq 0 ];then
|
||||
echo -e "\033[31m;1mDo not Use Root! \033[0m"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
source ~/.bash_profile
|
||||
|
||||
if [ ! -f $backup ];then
|
||||
mkdir -p $backup
|
||||
fi
|
||||
|
||||
pg_dump -h $host -p $port -U $username --no-password -d $database -F c -b -v -f $backup/$database-$now.bak
|
||||
|
||||
cd $backup && tar zcvf $database-$aweekago.tar.gz $database-$aweekago-* --remove-files
|
34
数据库/postgresql/postgresql-base-update.sh
Normal file
34
数据库/postgresql/postgresql-base-update.sh
Normal file
|
@ -0,0 +1,34 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Edit 2020/04/15
|
||||
|
||||
user=postgres
|
||||
source /home/$user/.bash_profile
|
||||
|
||||
backup=$PGDATA/../backup
|
||||
today=`date +%F`
|
||||
aweekago=`date -d -7days +%F`
|
||||
|
||||
host=10.1.139.126
|
||||
port=5432
|
||||
username=rep
|
||||
|
||||
if [ $UID -ne `id -u $user` ];then
|
||||
echo -e "\033[31;1mNot Manager of PostgreSQL! \033[0m"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# create base backup
|
||||
echo -e "\033[33;1m---$today---\nBack up Now!\n---\033[0m"
|
||||
if [ -f "$backup/$today/base" ];then
|
||||
mkdir -p $backup/$today/base
|
||||
fi
|
||||
pg_basebackup -D $backup/$today/base -h $host -p $port -Ft -z -P -v -w -U $username
|
||||
echo -e "\033[33;1m---\n---Finish---\033[0m"
|
||||
|
||||
# switch wal
|
||||
psql postgres -c "checkpoint;"
|
||||
psql postgres -c "select pg_switch_wal();"
|
||||
|
||||
# clean backup 7 days ago
|
||||
rm -rf $backup/$aweekago
|
BIN
数据库/postgresql/postgresql备份恢复数据.docx
Normal file
BIN
数据库/postgresql/postgresql备份恢复数据.docx
Normal file
Binary file not shown.
BIN
数据库/postgresql/postgresql定位分析消耗CPU高的SQL语句.docx
Normal file
BIN
数据库/postgresql/postgresql定位分析消耗CPU高的SQL语句.docx
Normal file
Binary file not shown.
0
数据库/postgresql/postgresql恢复误删数据.docx
Normal file
0
数据库/postgresql/postgresql恢复误删数据.docx
Normal file
BIN
数据库/postgresql/postgresql数据库相关操作.docx
Normal file
BIN
数据库/postgresql/postgresql数据库相关操作.docx
Normal file
Binary file not shown.
Binary file not shown.
BIN
数据库/postgresql/postgresql的pg_stat_statements扩展.docx
Normal file
BIN
数据库/postgresql/postgresql的pg_stat_statements扩展.docx
Normal file
Binary file not shown.
Binary file not shown.
1
数据库/postgresql/修改用户密码.txt
Normal file
1
数据库/postgresql/修改用户密码.txt
Normal file
|
@ -0,0 +1 @@
|
|||
alter user rep with password 'yanzhao';
|
1
数据库/postgresql/创建数据库.txt
Normal file
1
数据库/postgresql/创建数据库.txt
Normal file
|
@ -0,0 +1 @@
|
|||
CREATE DATABASE exampledb OWNER dbuser;
|
11
数据库/postgresql/创建用户.txt
Normal file
11
数据库/postgresql/创建用户.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
创建quartztstopr用户并赋权限
|
||||
|
||||
1、创建用户(create role 与create user 区别只有create user默认加上login,而create role不加)
|
||||
postgres=# create user quartztstopr with password 'Quartztstopr@2018';
|
||||
2、切换到quartztst数据库
|
||||
postgres=# \c quartztst
|
||||
3、为用户赋增删改查权限
|
||||
quartztst=# grant select,update,delete,insert on all tables in schema public to quartztstopr; # 查询,删除,更新,插入
|
||||
quartztst=# grant all privileges on all tables in schema public to quartztstopr; # 所有权限
|
||||
4、为用户赋修改表字段权限
|
||||
quartztst=# grant all privileges on all tables in schema public to quartztstopr;
|
45
数据库/postgresql/导入导出数据.txt
Normal file
45
数据库/postgresql/导入导出数据.txt
Normal file
|
@ -0,0 +1,45 @@
|
|||
1、导入整个数据库
|
||||
|
||||
# psql -U postgres(用户名) 数据库名(缺省时同用户名) < /data/dum.sql
|
||||
|
||||
|
||||
2、导出整个数据库
|
||||
|
||||
# pg_dump -h localhost -U postgres(用户名) 数据库名(缺省时同用户名) >/data/dum.sql
|
||||
|
||||
|
||||
3、导出某个表
|
||||
|
||||
# pg_dump -h localhost -U postgres(用户名) 数据库名(缺省时同用户名) -t table(表名) >/data/dum.sql
|
||||
|
||||
|
||||
4、压缩方法
|
||||
|
||||
一般用dump导出数据会比较大,推荐使用xz压缩
|
||||
|
||||
压缩方法 xz dum.sql 会生成 dum.sql.xz 的文件
|
||||
|
||||
|
||||
5、xz压缩数据倒数数据库方法
|
||||
|
||||
xzcat /data/dum.sql.xz | psql -h localhost -U postgres(用户名) 数据库名(缺省时同用户名)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
pg_dump -F c -f /dbbak/pgdumpbak/c.dmp -C -E UTF8 -h 127.0.0.1 -U postgres testdb #二进制格式备份文件
|
||||
|
||||
|
||||
|
||||
pg_dump -F p -f /dbbak/pgdumpbak/p.dmp -C -E UTF8 -h 127.0.0.1 -U postgres testdb #文本格式备份文件,”-C” 表示包含创建语句
|
||||
|
||||
n
|
||||
|
||||
pg_restore /dbbak/c.dmp|less 可以解析二进制格式的备份文件
|
||||
|
||||
pg_restore -l /dbbak/c.dmp
|
||||
|
||||
pg_restore -d testdb /dbbak/pgdumpbak/c.dmp #需要先创建目标库
|
||||
|
||||
pg_restore -d postgres /dbbak/pgdumpbak/p.dmp #文件中包含创建数据库的命令,不需要创建目标库
|
7
数据库/postgresql/批量修改表或视图的owner.txt
Normal file
7
数据库/postgresql/批量修改表或视图的owner.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
DO $$DECLARE r record;
|
||||
BEGIN
|
||||
FOR r IN SELECT tablename/viewname FROM pg_tables/pg_views WHERE schemaname = 'public'
|
||||
LOOP
|
||||
EXECUTE 'alter table '|| r.tablename/r.viewname ||' owner to new_owner;';
|
||||
END LOOP;
|
||||
END$$;
|
9
数据库/postgresql/查询所有库名.txt
Normal file
9
数据库/postgresql/查询所有库名.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
查询所有库名
|
||||
\l
|
||||
或
|
||||
SELECT datname FROM pg_database;
|
||||
|
||||
查询当前库下有哪些表
|
||||
\dt
|
||||
或者
|
||||
SELECT tablename FROM pg_tables;
|
6
数据库/postgresql/查询用户的权限.txt
Normal file
6
数据库/postgresql/查询用户的权限.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
查询cardbtstopr的权限
|
||||
|
||||
1、切换到cardbtst数据库
|
||||
postgres=# \c cardbtst
|
||||
2、查询权限
|
||||
cardbtst=# select * from information_schema.role_table_grants where grantee='cardbtstopr';
|
356
数据库/postgresql/理解PostgreSQL中的权限.txt
Normal file
356
数据库/postgresql/理解PostgreSQL中的权限.txt
Normal file
|
@ -0,0 +1,356 @@
|
|||
在postgresql中,database,schema,table之间关系
|
||||
从逻辑上看,schema、table都是位于database之下。在postgres数据库下建立表(相当于建立在public schema下),理解pg下的权限:
|
||||
\dp - lists table/view permissions
|
||||
\dn+ - lists schema permissions
|
||||
\l+ does not list all users that can access the database
|
||||
|
||||
using psql from postgres 8.4 and postgres 9.0, and the command \l or \l+ gives me column Access Privileges where I have entry:
|
||||
<user_name>=c/<database_name>
|
||||
|
||||
\dp显示的项解释如下:
|
||||
|
||||
角色名=xxxx -- 被授予给一个角色的特权
|
||||
=xxxx -- 被授予给 PUBLIC 的特权
|
||||
|
||||
r -- SELECT ("读")
|
||||
w -- UPDATE ("写")
|
||||
a -- INSERT ("追加")
|
||||
d -- DELETE
|
||||
D -- TRUNCATE
|
||||
x -- REFERENCES
|
||||
t -- TRIGGER
|
||||
X -- EXECUTE
|
||||
U -- USAGE
|
||||
C -- CREATE
|
||||
c -- CONNECT
|
||||
T -- TEMPORARY
|
||||
arwdDxt -- ALL PRIVILEGES (对于表,对其他对象会变化)
|
||||
* -- 用于前述特权的授权选项
|
||||
|
||||
/yyyy -- 授予该特权的角色
|
||||
|
||||
在PostgreSQL中,可以把角色划分为如下几类:
|
||||
|
||||
SELECT:该权限用来查询表或是表上的某些列,或是视图,序列。
|
||||
INSERT:该权限允许对表或是视图进行插入数据操作,也可以使用COPY FROM进行数据的插入。
|
||||
UPDATE:该权限允许对表或是或是表上特定的列或是视图进行更新操作。
|
||||
DELETE:该权限允许对表或是视图进行删除数据的操作。
|
||||
TRUNCATE:允许对表进行清空操作。
|
||||
REFERENCES:允许给参照列和被参照列上创建外键约束。
|
||||
TRIGGER:允许在表上创建触发器。
|
||||
CREATE:对于数据库,允许在数据库上创建Schema;对于Schema,允许对Schema上创建数据库对象;对于表空间,允许把表或是索引指定到对应的表空间上。
|
||||
CONNECT:允许用户连接到指定的数据库上。
|
||||
TEMPORARY或是TEMP:允许在指定数据库的时候创建临时表。
|
||||
EXECUTE:允许执行某个函数。
|
||||
USAGE:对于程序语言来说,允许使用指定的程序语言创建函数;对于Schema来说,允许查找该Schema下的对象;对于序列来说,允许使用currval和nextval函数;对于外部封装器来说,允许使用外部封装器来创建外部服务器;对于外部服务器来说,允许创建外部表。
|
||||
ALL PRIVILEGES:表示一次性给予可以授予的权限。
|
||||
|
||||
创建两个测试账号
|
||||
create role web login connection limit 9 password 'web_app';
|
||||
create role mobile login connection limit 9 password 'mob_ile';
|
||||
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA dba GRANT SELECT ON TABLES TO mobile;
|
||||
没有起到作用,用下面的语句:
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA dba TO mobile;
|
||||
|
||||
从dba终端看是有权限的,但mobile中用\dpp却看不到,难道要重新登录一下?
|
||||
|
||||
重新登录后也看不到,试试下面的规则:
|
||||
GRANT CONNECT ON DATABASE dba TO mobile;
|
||||
GRANT USAGE ON SCHEMA dba TO mobile;
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA dba TO mobile;
|
||||
GRANT SELECT ON ALL SEQUENCES IN SCHEMA dba to mobile;
|
||||
单表授权
|
||||
GRANT SELECT,INSERT,UPDATE ON TABLE web9 TO web;
|
||||
|
||||
成功。
|
||||
|
||||
后来发现是search_path这个变量的问题,在当前的变量中没有包含dba,所以\dpp时看不见,但从其中的表中取数据是可以的:select * from dba.table limit 9;
|
||||
|
||||
If your read-only user doesn't have permission to list tables (i.e. \d returns no results), it's probably because you don't have USAGE permissions for the schema. USAGE is a permission that allows users to actually use the permissions they have been assigned. What's the point of this? I'm not sure. To fix:
|
||||
# You can either grant USAGE to everyone
|
||||
GRANT USAGE ON SCHEMA public TO public;
|
||||
|
||||
# Or grant it just to your read only user
|
||||
GRANT USAGE ON SCHEMA public TO readonlyuser;
|
||||
|
||||
授权首先要能连接(connection)到数据库(pg_hba.conf)上,在模式上要有使用权限(usage),然后是其下的对象的操作权限(如果该对象可以再度分割,如表中有列,对列赋予不同的权限);可以对用户在给定的模式下设定权限(可批可零),或对给定的模式中指定默认的操作权限(批量)。
|
||||
|
||||
撤销权限
|
||||
REVOKE CREATE ON SCHEMA public FROM public;
|
||||
|
||||
第一个 "public" 是模式,第二个 "public" 意思是"所有用户"。
|
||||
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA PUBLIC to freeoa_role; --赋予freeoa_role所有表的SELECT权限
|
||||
|
||||
特殊符号:ALL代表所访问权限,PUBLIC代表所有用户。
|
||||
|
||||
要把新的模式放到路径中来,我们用:
|
||||
SET search_path TO myschema,"$user",public;
|
||||
|
||||
仅对本次会话有效,下次登录又要设置一下。
|
||||
|
||||
或者修改用户的搜索路径,这样即使下次登录也不用重新设置:
|
||||
ALTER USER mobile SET search_path=dba, "$user",public;
|
||||
|
||||
修改模式默认的权限
|
||||
alter default privileges in schema public grant select on tables to web;
|
||||
|
||||
比较通用的模式下对象授权方式多采用:先移除所有用户的所有权限,再有针对性的授权。
|
||||
REVOKE ALL ON ALL TABLES IN SCHEMA public FROM PUBLIC;
|
||||
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO user_name;
|
||||
|
||||
或者修改具体用户的默认权限
|
||||
ALTER DEFAULT PRIVILEGES
|
||||
FOR ROLE some_role -- Alternatively "FOR USER"
|
||||
IN SCHEMA public
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO user_name;
|
||||
|
||||
这是一个很开放的权限
|
||||
grant all privileges on database dbname to dbuser;
|
||||
|
||||
grant是赋予用户schema下当前表的权限,alter default privileges是赋予用户schema下表的默认权限,这样以后新建表就不用再赋权限了。当我们创建只读账号的时候,需要执行grant和alter default privileges。其次这样可很好地解决每次新建表就要赋一次权限的问题了。
|
||||
|
||||
alter default privileges in schema dba grant select,insert,update,delete on tables to mobile;
|
||||
|
||||
创建的普通用户默认是没有任何权限的。查看表等对象的权限可通过:\dpp来查看,相当直观。
|
||||
|
||||
序列的权限与解决办法
|
||||
|
||||
在insert的时候,指定列插入,主键id是serial类型会默认走sequence的下一个值,但前面只赋予了表的权限,所以会出现下面的问题:
|
||||
|
||||
postgres=> insert into t4 ( name ) values ( 'aa' );
|
||||
ERROR: permission denied for sequence t4_id_seq
|
||||
|
||||
解决方法就是再赋一次sequence的值就行了
|
||||
alter default privileges in schema public grant usage on sequences to user2;
|
||||
|
||||
删除用户
|
||||
|
||||
删除用户和组
|
||||
|
||||
删除用户和组很简单:
|
||||
DROP ROLE role_name;
|
||||
DROP ROLE IF EXISTS role_name;
|
||||
|
||||
删除组role只会删除组的role本身,组的成员并不会被删除。
|
||||
|
||||
postgres=> \c - postgres
|
||||
You are now connected to database "postgres" as user "postgres".
|
||||
postgres=# drop role user2;
|
||||
ERROR: role "user2" cannot be dropped because some objects depend on it
|
||||
DETAIL: privileges for table t5
|
||||
privileges for sequence t5_id_seq
|
||||
privileges for default privileges on new sequences belonging to role postgres in schema public
|
||||
privileges for table t4
|
||||
privileges for default privileges on new relations belonging to role postgres in schema public
|
||||
|
||||
当我们删除用户的时候,会提示有权限依赖,所以我们要删除这些权限
|
||||
|
||||
postgres=# alter default privileges in schema public revoke usage on sequences from user2;
|
||||
ALTER DEFAULT PRIVILEGES
|
||||
postgres=# alter default privileges in schema public revoke select,insert,delete,update on tables from user2;
|
||||
ALTER DEFAULT PRIVILEGES
|
||||
postgres=# revoke select,insert,delete,update on all tables in schema public from user2;
|
||||
REVOKE
|
||||
postgres=# revoke usage on all sequences in schema public from user2;
|
||||
REVOKE
|
||||
postgres=# drop role user2;
|
||||
DROP ROLE
|
||||
|
||||
Pg权限分为两部分,一部分是“系统权限”或者数据库用户的属性,可以授予role或user(两者区别在于login权限);一部分为数据库对象上的操作权限。对超级用户不做权限检查,其它走acl。对于数据库对象,开始只有所有者和超级用户可以做任何操作,其它走acl。在pg里,对acl模型做了简化,组和角色都是role。数据库对象上的权限有:SELECT,INSERT,UPDATE,DELETE,RULE,REFERENCES,TRIGGER,CREATE,TEMPORARY,EXECUTE 和 USAGE等。
|
||||
|
||||
可以用特殊的名字 PUBLIC 把对象的权限赋予系统中的所有角色。 在权限声明的位置上写 ALL,表示把适用于该对象的所有权限都赋予目标角色。
|
||||
|
||||
视图 pg_roles提供访问数据库角色有关信息的接口。 它只是一个 pg_authid 表的公开可读部分的视图,把口令字段用空白填充了。
|
||||
|
||||
pg_roles字段
|
||||
名字
|
||||
|
||||
类型
|
||||
|
||||
引用
|
||||
|
||||
描述
|
||||
|
||||
rolname
|
||||
|
||||
name
|
||||
|
||||
|
||||
|
||||
角色名
|
||||
|
||||
rolsuper
|
||||
|
||||
bool
|
||||
|
||||
|
||||
|
||||
有超级用户权限的角色
|
||||
|
||||
rolcreaterole
|
||||
|
||||
bool
|
||||
|
||||
|
||||
|
||||
可以创建更多角色的角色
|
||||
|
||||
rolcreatedb
|
||||
|
||||
bool
|
||||
|
||||
|
||||
|
||||
可以创建数据库的角色
|
||||
|
||||
rolcatupdate
|
||||
|
||||
bool
|
||||
|
||||
|
||||
|
||||
可以直接更新系统表的角色。(除非这个字段为真,否则超级用户也不能干这个事情。)
|
||||
|
||||
rolcanlogin
|
||||
|
||||
bool
|
||||
|
||||
|
||||
|
||||
可以登录的角色,也就是说,这个角色可以给予初始化会话认证的标识符。
|
||||
|
||||
rolpassword
|
||||
|
||||
text
|
||||
|
||||
|
||||
|
||||
不是口令(总是 ********)
|
||||
|
||||
rolvaliduntil
|
||||
|
||||
timestamptz
|
||||
|
||||
|
||||
|
||||
口令失效日期(只用于口令认证);如果没有失效期,为 NULL
|
||||
|
||||
rolconfig
|
||||
|
||||
text[]
|
||||
|
||||
|
||||
|
||||
运行时配置变量的会话缺省
|
||||
|
||||
|
||||
|
||||
角色属性(Role Attributes)
|
||||
|
||||
一个数据库角色可以有一系列属性,这些属性定义了他的权限。
|
||||
|
||||
属性 说明
|
||||
login 只有具有 LOGIN 属性的角色可以用做数据库连接的初始角色名。
|
||||
superuser 数据库超级用户
|
||||
createdb 创建数据库权限
|
||||
createrole 允许其创建或删除其他普通的用户角色(超级用户除外)
|
||||
replication 做流复制的时候用到的一个用户属性,一般单独设定。
|
||||
password 在登录时要求指定密码时才会起作用,比如md5或者password模式,跟客户端的连接认证方式有关
|
||||
inherit 用户组对组员的一个继承标志,成员可以继承用户组的权限特性
|
||||
|
||||
在psql中的查看权限的快捷指令
|
||||
|
||||
\dn[S+] [PATTERN] 列出所有模式
|
||||
|
||||
\dp [模式] 列出表,视图和序列的访问权限,同\z
|
||||
|
||||
\du[S+] [PATTERN] 列出角色
|
||||
|
||||
\ddp [模式] 列出默认权限
|
||||
|
||||
\drds [模式1 [模式2]] 列出每个数据库的角色设置
|
||||
|
||||
database、schema、table_seq_view_etc、table_column 分4个级别来授权。
|
||||
|
||||
查看pg_hba.conf 文件,在角色属性中关于password的说明,在登录时要求指定密码时才会起作用,比如md5或者password模式,跟客户端的连接认证方式有关。
|
||||
|
||||
给已存在用户赋权限
|
||||
|
||||
使用ALTER ROLE 命令。
|
||||
ALTER ROLE name RENAME TO new_name
|
||||
|
||||
ALTER ROLE name [ IN DATABASE database_name ] SET configuration_parameter { TO | = } { value | DEFAULT }
|
||||
ALTER ROLE name [ IN DATABASE database_name ] SET configuration_parameter FROM CURRENT
|
||||
ALTER ROLE name [ IN DATABASE database_name ] RESET configuration_parameter
|
||||
ALTER ROLE name [ IN DATABASE database_name ] RESET ALL
|
||||
|
||||
为角色成员赋权
|
||||
|
||||
查看角色信息
|
||||
|
||||
psql 终端可以用\du 或\du+ 查看,也可以查看系统表
|
||||
select * from pg_roles;
|
||||
select * from pg_user;
|
||||
|
||||
在系统的角色管理中,通常会把多个角色赋予一个组,这样在设置权限时只需给该组设置即可,撤销权限时也是从该组撤销。在PostgreSQL中,首先需要创建一个代表组的角色,之后再将该角色的membership 权限赋给独立的角色即可。
|
||||
|
||||
创建组角色
|
||||
# CREATE ROLE father login nosuperuser nocreatedb nocreaterole noinherit encrypted password 'freeoa';
|
||||
|
||||
给father 角色赋予数据库test 连接权限和相关表的查询权限。
|
||||
# GRANT CONNECT ON DATABASE test to father;
|
||||
test=> GRANT USAGE ON SCHEMA public to father;
|
||||
WARNING: no privileges were granted for "public"
|
||||
|
||||
test=> GRANT SELECT on public.emp to father;
|
||||
|
||||
创建成员角色
|
||||
test=> \c postgres postgres
|
||||
You are now connected to database "postgres" as user "postgres".
|
||||
# CREATE ROLE son1 login nosuperuser nocreatedb nocreaterole inherit encrypted password 'freeoa.net';
|
||||
|
||||
这里创建了son1 角色,并开启inherit 属性。PostgreSQL 里的角色赋权是通过角色继承(INHERIT)的方式实现的。
|
||||
|
||||
将father 角色赋给son1
|
||||
# GRANT father to son1;
|
||||
|
||||
还有另一种方法,就是在创建用户的时候赋予角色权限。
|
||||
# CREATE ROLE son2 login nosuperuser nocreatedb nocreaterole inherit encrypted password 'freeoa.net' in role father;
|
||||
|
||||
用户在public模式下创建的表对于其它用户能看到,但查不了,会报"对关系 prv 权限不够",除非你是这个库的属主。
|
||||
|
||||
可以通过函数来验证模式下的表的相应权限:
|
||||
select has_table_privilege('public.table1','select');
|
||||
select has_table_privilege('dba.webcon_cid_seq','update');
|
||||
|
||||
对sequence类型的授权
|
||||
select 什么都做不了,usage有currval,nextval这两个函数可用,setval不可用,要使用setval就必须有update权限。
|
||||
|
||||
grant usage on sequence web_cid_seq to some_user;
|
||||
|
||||
切换ROLE
|
||||
|
||||
SET ROLE role_name; --切换到role_name用户
|
||||
RESET ROLE; --切换回最初的role
|
||||
|
||||
INHERIT权限:该属性使组成员拥有组的所有权限
|
||||
|
||||
ALTER ROLE freeoa_user INHERIT;
|
||||
|
||||
通过以下方式禁止用户登录
|
||||
|
||||
ALTER ROLE username WITH NOLOGIN;
|
||||
|
||||
第三方的小工具
|
||||
|
||||
somebody created a convenient script for that; pg_grant_read_to_db.sh. This script grants read-only privileges to a specified role on all tables, views and sequences in a database schema and sets them as default.
|
||||
url:https://gist.github.com/jirutka/afa3ce62b1430abf7572
|
||||
|
||||
|
||||
|
||||
参考来源
|
||||
GRANT[http://postgres.cn/docs/9.5/sql-grant.html]
|
||||
ALTER DEFAULT PRIVILEGES[http://postgres.cn/docs/9.5/sql-alterdefaultprivileges.html]
|
310
数据库/postgresql/用户模式权限基础.txt
Normal file
310
数据库/postgresql/用户模式权限基础.txt
Normal file
|
@ -0,0 +1,310 @@
|
|||
一.USER用户管理
|
||||
1.查看用户
|
||||
pg中的role,user,group基本是一样的,只是默认创建的role,group没有登录数据库的权限.用户分为普通用户和超级用户
|
||||
1.使用\du查看数据库中的用户,其中role name是用户名,第二列是用户的属性,第三列表示用户具有哪些成员,例如将suq赋予给brent
|
||||
|
||||
postgres=# \du
|
||||
List of roles
|
||||
Role name | Attributes | Member of
|
||||
-----------+------------------------------------------------------------+-----------
|
||||
brent | | {suq}
|
||||
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
|
||||
suq | 1 connection | {}
|
||||
zdry | Superuser +| {}
|
||||
| Password valid until infinity |
|
||||
2.创建用户
|
||||
1.查看创建用户的语法
|
||||
|
||||
test=# \h create user
|
||||
Command: CREATE USER
|
||||
Description: define a new database role
|
||||
Syntax:
|
||||
CREATE USER name [ [ WITH ] option [ ... ] ]
|
||||
where option can be:
|
||||
SUPERUSER | NOSUPERUSER
|
||||
| CREATEDB | NOCREATEDB
|
||||
| CREATEROLE | NOCREATEROLE
|
||||
| INHERIT | NOINHERIT --继承
|
||||
| LOGIN | NOLOGIN
|
||||
| REPLICATION | NOREPLICATION
|
||||
| BYPASSRLS | NOBYPASSRLS
|
||||
| CONNECTION LIMIT connlimit
|
||||
| [ ENCRYPTED ] PASSWORD 'password'
|
||||
| VALID UNTIL 'timestamp'
|
||||
| IN ROLE role_name [, ...]
|
||||
| IN GROUP role_name [, ...]
|
||||
| ROLE role_name [, ...]
|
||||
| ADMIN role_name [, ...]
|
||||
| USER role_name [, ...]
|
||||
| SYSID uid
|
||||
2.创建一个普通用户
|
||||
|
||||
postgres=# create user test ENCRYPTED password 'test';
|
||||
CREATE ROLE
|
||||
3.为创建一个超级用户
|
||||
|
||||
test=# create user dsg superuser;
|
||||
CREATE ROLE
|
||||
4.创建一个普通用户,并且赋予相关权限
|
||||
|
||||
test=# create user dsg createdb createrole inherit password 'dsg';
|
||||
CREATE ROLE
|
||||
3.修改用户
|
||||
1.查看修改用户语句
|
||||
|
||||
test=# \h alter user
|
||||
Command: ALTER USER
|
||||
Description: change a database role
|
||||
Syntax:
|
||||
ALTER USER role_specification [ WITH ] option [ ... ]
|
||||
where option can be:
|
||||
SUPERUSER | NOSUPERUSER
|
||||
| CREATEDB | NOCREATEDB
|
||||
| CREATEROLE | NOCREATEROLE
|
||||
| INHERIT | NOINHERIT
|
||||
| LOGIN | NOLOGIN
|
||||
| REPLICATION | NOREPLICATION
|
||||
| BYPASSRLS | NOBYPASSRLS
|
||||
| CONNECTION LIMIT connlimit
|
||||
| [ ENCRYPTED ] PASSWORD 'password'
|
||||
| VALID UNTIL 'timestamp'
|
||||
ALTER USER name RENAME TO new_name
|
||||
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter { TO | = } { value | DEFAULT }
|
||||
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] SET configuration_parameter FROM CURRENT
|
||||
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] RESET configuration_parameter
|
||||
ALTER USER { role_specification | ALL } [ IN DATABASE database_name ] RESET ALL
|
||||
where role_specification can be:
|
||||
role_name
|
||||
| CURRENT_USER
|
||||
| SESSION_USER
|
||||
2.修改用户为超级用户
|
||||
|
||||
postgres=# alter user test superuser;
|
||||
ALTER ROLE
|
||||
3.将超级用户修改为普通用户
|
||||
|
||||
postgres=# alter user test nosuperuser;
|
||||
ALTER ROLE
|
||||
4.修改用户密码
|
||||
|
||||
test=# alter user dsg password 'test';
|
||||
ALTER ROLE
|
||||
5.修改用户名
|
||||
|
||||
test=# alter user dsg rename to dds;
|
||||
NOTICE: MD5 password cleared because of role rename
|
||||
ALTER ROLE
|
||||
6.锁定/解锁用户,不允许/允许其登录
|
||||
|
||||
test=# alter user test nologin;
|
||||
ALTER ROLE
|
||||
test=# alter user test login;
|
||||
ALTER ROLE
|
||||
7.设置用户的连接数,其中0表示不允许登录,-1表示无限制
|
||||
|
||||
test=# alter user test connection limit 10;
|
||||
ALTER ROLE
|
||||
4.删除用户
|
||||
1.直接删除用户
|
||||
|
||||
test=# drop user dds;
|
||||
DROP ROLE
|
||||
如果用户在数据库中有相关对象,不能直接删除,需要将相关对象所属修改到其它用户中
|
||||
|
||||
test=# drop user dsg;
|
||||
ERROR: role "dsg" cannot be dropped because some objects depend on it
|
||||
DETAIL: owner of table zzz.kkk
|
||||
privileges for schema zzz
|
||||
将dsg的所属用户修改为test:
|
||||
|
||||
test=# reassign owned by dsg to test;
|
||||
REASSIGN OWNED
|
||||
还需要把权限进行收回,再进行删除:
|
||||
|
||||
test=# revoke all on schema zzz from dsg;
|
||||
REVOKE
|
||||
test=# drop user dsg;
|
||||
DROP ROLE
|
||||
|
||||
二.schema模式管理
|
||||
首先介绍一下postgresql中的schema,postgresql中的schema和其它关系型数据库中的schema含义是一致的,在oracle中叫schema或者用户,只是oracle中schema和用户是始终一一对应.
|
||||
在mysql中database和schema是一一对应的.postgresql中user和schema是可以不一致的,相对比其它数据库复杂一点.
|
||||
在创建schema的时候,可以指定schema的所属用户,默认的只有所属用户和超级用户才能在此schema进行对象操作,否则就需要授权.
|
||||
1.使用\dn查看数据库的schema
|
||||
|
||||
test=# \dn
|
||||
List of schemas
|
||||
Name | Owner
|
||||
--------+----------
|
||||
brent | brent
|
||||
public | postgres
|
||||
suq | suq
|
||||
zzz | test
|
||||
(4 rows)
|
||||
|
||||
2.创建schema
|
||||
1.查看创建schema语法
|
||||
|
||||
test=# \h create schema
|
||||
Command: CREATE SCHEMA
|
||||
Description: define a new schema
|
||||
Syntax:
|
||||
CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]
|
||||
CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]
|
||||
CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]
|
||||
CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification
|
||||
where role_specification can be:
|
||||
user_name
|
||||
| CURRENT_USER
|
||||
| SESSION_USER
|
||||
2.创建一个schema,并且设置所属用户为test:
|
||||
|
||||
test=# create schema zzz authorization test;
|
||||
CREATE SCHEMA
|
||||
|
||||
3.删除schema
|
||||
1.删除schema,如果schema中存在对象,则需要使用cascade选项:
|
||||
|
||||
test=# drop schema zzz;
|
||||
ERROR: cannot drop schema zzz because other objects depend on it
|
||||
DETAIL: table zzz.test depends on schema zzz
|
||||
HINT: Use DROP ... CASCADE to drop the dependent objects too.
|
||||
test=# drop schema zzz cascade;
|
||||
NOTICE: drop cascades to table zzz.test
|
||||
DROP SCHEMA
|
||||
|
||||
三.权限管理
|
||||
postgresql中的权限可以大概分为以下几种:
|
||||
SELECT:该权限用来查询表或是表上的某些列,或是视图,序列。
|
||||
INSERT:该权限允许对表或是视图进行插入数据操作,也可以使用COPY FROM进行数据的插入。
|
||||
UPDATE:该权限允许对表或是或是表上特定的列或是视图进行更新操作。
|
||||
DELETE:该权限允许对表或是视图进行删除数据的操作。
|
||||
TRUNCATE:允许对表进行清空操作。
|
||||
REFERENCES:允许给参照列和被参照列上创建外键约束。
|
||||
TRIGGER:允许在表上创建触发器。
|
||||
CREATE:对于数据库,允许在数据库上创建Schema;对于Schema,允许对Schema上创建数据库对象;对于表空间,允许把表或是索引指定到对应的表空间上。
|
||||
CONNECT:允许用户连接到指定的数据库上。
|
||||
TEMPORARY或是TEMP:允许在指定数据库的时候创建临时表。
|
||||
EXECUTE:允许执行某个函数。
|
||||
USAGE:对于程序语言来说,允许使用指定的程序语言创建函数;对于Schema来说,允许查找该Schema下的对象;对于序列来说,允许使用currval和nextval函数;对于外部封装器来说,允许使用外部封装器来创建外部服务器;对于外部服务器来说,允许创建外部表。
|
||||
ALL PRIVILEGES:表示一次性给予可以授予的权限。
|
||||
|
||||
1.schema权限管理
|
||||
首先,如果某个用户需要访问某张表,那么用户首先需要有访问该表所在schema的权限.默认只有schema的所属可以直接操作该schema,其它用户需要授权(public schma除外)
|
||||
1.将schema的权限赋予给指定用户
|
||||
例如,将创建对象权限赋予给brent用户:
|
||||
|
||||
test=# grant create on schema zzz to brent;
|
||||
GRANT
|
||||
例如,将schema中usage权限赋予给brent用户:
|
||||
|
||||
test=> grant usage on schema zzz to brent;
|
||||
GRANT
|
||||
例如,将schema中all权限赋予给brent用户,all表示一次性给予可以授予的所有权限
|
||||
|
||||
test=> grant all on schema zzz to brent;
|
||||
GRANT
|
||||
|
||||
2.表权限管理
|
||||
默认的,如果没有特别的授权,普通用户只能访问表所属为自己的表.超级用户可以访问任何表.如果要访问非自己的表,那么就需要对表进行授权.
|
||||
当我们以brent用户想访问zzz模式下所属用户为test的abc表的时候就会报错:
|
||||
|
||||
test=> select user;
|
||||
user
|
||||
-------
|
||||
brent
|
||||
test=> select * from zzz.abc;
|
||||
ERROR: permission denied for relation abc
|
||||
1.grant,将表的查询和插入权限赋予给brent:
|
||||
|
||||
test=# grant select,insert on zzz.abc to brent;
|
||||
GRANT
|
||||
那么就可以进行查询了:
|
||||
|
||||
(1 row)
|
||||
test=> \c test brent
|
||||
You are now connected to database "test" as user "brent".
|
||||
test=> select * from zzz.abc;
|
||||
id
|
||||
----
|
||||
(0 rows)
|
||||
2.revoke,将表的查询权限收回:
|
||||
|
||||
test=# set search_path=zzz;
|
||||
SET
|
||||
test=# \dt
|
||||
List of relations
|
||||
Schema | Name | Type | Owner
|
||||
--------+------+-------+-------
|
||||
zzz | abc | table | test
|
||||
zzz | kkk | table | test
|
||||
(2 rows)
|
||||
test=# revoke select on zzz.abc from brent;
|
||||
REVOKE
|
||||
|
||||
3.角色管理
|
||||
我们除了可以将表的权限赋予给用户,我们还可以将角色赋予给用户,那么用户就会拥有赋予角色的相关权限:
|
||||
|
||||
test=# grant test to brent;
|
||||
GRANT ROLE
|
||||
test=# revoke test from brent;
|
||||
REVOKE ROLE
|
||||
|
||||
4.查询表权限角色列表
|
||||
使用\dp或者\z命令,可以查看表对象上已经分配的权限列表,如下:
|
||||
|
||||
test=# \dp abc
|
||||
Access privileges
|
||||
Schema | Name | Type | Access privileges | Column privileges | Policies
|
||||
--------+------+-------+-------------------+-------------------+----------
|
||||
zzz | abc | table | test=arwdDxt/test+| |
|
||||
| | | brent=a/test +| |
|
||||
| | | uuu=arwdDxt/test | |
|
||||
(1 row)
|
||||
详细的权限说明如下:
|
||||
r -- SELECT ("读")
|
||||
w -- UPDATE ("写")
|
||||
a -- INSERT ("追加")
|
||||
d -- DELETE
|
||||
D -- TRUNCATE
|
||||
x -- REFERENCES
|
||||
t -- TRIGGER
|
||||
X -- EXECUTE
|
||||
U -- USAGE
|
||||
C -- CREATE
|
||||
c -- CONNECT
|
||||
T -- TEMPORARY
|
||||
arwdDxt -- ALL PRIVILEGES (对于表,对其他对象会变化)
|
||||
* -- 用于前述特权的授权选项
|
||||
/yyyy -- 授予该特权的角色
|
||||
|
||||
使用\du可以查看角色之间的成员关系:
|
||||
|
||||
test=# \du brent
|
||||
List of roles
|
||||
Role name | Attributes | Member of
|
||||
-----------+------------+------------
|
||||
brent | | {uuu,test}
|
||||
其中uuu,test是brent的成员,也就是说uuu,test角色被赋予给了brent用户.\du类似与查看oracle中dba_role_privs
|
||||
当我们决定收回某个表给予某个用户的权限的时候,除了需要收回表的权限,还需要检查用户的角色信息,保证用户的角色也没有相关的权限.
|
||||
|
||||
还可以通过查询information_schema.role_table_grants来了解某个用户具有的权限,类似于oralce中dba_tab[sys]_privs
|
||||
test=# select * from information_schema.role_table_grants where grantee='brent';
|
||||
grantor | grantee | table_catalog | table_schema | table_name | privilege_type | is_grantable | with_hierarchy
|
||||
---------+---------+---------------+--------------+------------+----------------+--------------+----------------
|
||||
brent | brent | test | brent | x | INSERT | YES | NO
|
||||
brent | brent | test | brent | x | SELECT | YES | YES
|
||||
brent | brent | test | brent | x | UPDATE | YES | NO
|
||||
brent | brent | test | brent | x | DELETE | YES | NO
|
||||
brent | brent | test | brent | x | TRUNCATE | YES | NO
|
||||
brent | brent | test | brent | x | REFERENCES | YES | NO
|
||||
brent | brent | test | brent | x | TRIGGER | YES | NO
|
||||
brent | brent | test | brent | tt | INSERT | YES | NO
|
||||
brent | brent | test | brent | tt | SELECT | YES | YES
|
||||
brent | brent | test | brent | tt | UPDATE | YES | NO
|
||||
brent | brent | test | brent | tt | DELETE | YES | NO
|
||||
brent | brent | test | brent | tt | TRUNCATE | YES | NO
|
||||
brent | brent | test | brent | tt | REFERENCES | YES | NO
|
||||
brent | brent | test | brent | tt | TRIGGER | YES | NO
|
||||
test | brent | test | zzz | abc | INSERT | YES | NO
|
||||
(15 rows)
|
115
数据库/postgresql/连接用完问题.txt
Normal file
115
数据库/postgresql/连接用完问题.txt
Normal file
|
@ -0,0 +1,115 @@
|
|||
[postgres@vd ~]$ psql -d mydb -U appuser
|
||||
|
||||
psql: FATAL: remaining connection slots are reserved for non-replication superuser connections
|
||||
|
||||
#使用普通用户登录到数据库的时候数据库报错。并且拒绝链接访问。
|
||||
|
||||
[postgres@vd ~]$ psql -U postgres -d mydb
|
||||
|
||||
psql (9.5.4)
|
||||
|
||||
Type "help" for help.
|
||||
|
||||
mydb=# q
|
||||
|
||||
#而使用超级用户登录到该数据库却并没有提示错误。
|
||||
|
||||
[postgres@vd ~]$ psql -U appuser -d mydb
|
||||
|
||||
psql: FATAL: remaining connection slots are reserved for non-replication superuser connections
|
||||
|
||||
[postgres@vd ~]$ psql -U postgres -d mydb
|
||||
|
||||
psql (9.5.4)
|
||||
|
||||
Type "help" for help.
|
||||
|
||||
mydb=> select datname,datconnlimit from pg_database ;
|
||||
|
||||
datname | datconnlimit
|
||||
|
||||
-----------+--------------
|
||||
|
||||
template1 | -1
|
||||
|
||||
template0 | -1
|
||||
|
||||
postgres | -1
|
||||
|
||||
mydb | -1
|
||||
|
||||
(4 rows)
|
||||
|
||||
#数据库链接数并没有进行限制。也就是说链接上线不是数据库自身设置抛出。
|
||||
|
||||
mydb=# select count(*) from pg_stat_activity ;
|
||||
|
||||
count
|
||||
|
||||
-------
|
||||
|
||||
402
|
||||
|
||||
(1 row)
|
||||
|
||||
mydb=# select current_setting('max_connections');
|
||||
|
||||
current_setting
|
||||
|
||||
-----------------
|
||||
|
||||
410
|
||||
|
||||
(1 row)
|
||||
|
||||
mydb=# select current_setting('superuser_reserved_connections');
|
||||
|
||||
current_setting
|
||||
|
||||
-----------------
|
||||
|
||||
10
|
||||
|
||||
(1 row)
|
||||
|
||||
#用超级用户登录上去检查一下链接数是否正常。
|
||||
|
||||
#max_connections是总链接数,
|
||||
|
||||
#superuser_reserved_connections是为超级用户预留的用户数,
|
||||
|
||||
#也就是说 :普通用户最多可以登录数量=max_connections-superuser_reserved_connections
|
||||
|
||||
mydb=# select pg_terminate_backend(pid) from pg_stat_activity where pid<>pg_backend_pid() and pg_stat_activity.state='idle';
|
||||
|
||||
pg_terminate_backend
|
||||
|
||||
----------------------
|
||||
|
||||
t
|
||||
|
||||
...(省略)...
|
||||
|
||||
t
|
||||
|
||||
(352 row)
|
||||
|
||||
#经过协调,相关人员同意将空闲用户都清除出去。使用pg_terminate_backend函数可以从数据库服务器端直接断开这些空闲链接。
|
||||
|
||||
#当然我自己的链接不能被断开。pg_backend_pid()是自己的pid号。
|
||||
|
||||
mydb=# q
|
||||
|
||||
[postgres@vd ~]$ psql -U appuser -d mydb
|
||||
|
||||
psql (9.5.4)
|
||||
|
||||
Type "help" for help.
|
||||
|
||||
mydb=> q
|
||||
|
||||
#再次使用普通用户链接就没有这个问题了。
|
||||
|
||||
追其发生原因,是业务方面不断增加任务需求,而开发人员为了增加任务同时工作数量,在中间件上不断增加链接数,而数据库端却没有增加,不增加数据库上的总链接数是因为怕数据库端内存不够而不敢无休止的增加,希望中间件能协调好复用链接,但没想到中间件最后解决的方法是直接占满数据库链接,起始中间件的日志中也有不少该报错,只是没有发现。
|
||||
|
||||
这里不得不吐嘈一下,目前postgresql数据库还没有共享链接方式,只能是来一个链接起一个进程(非windows),一般是使用中间件来控制链接过多的问题,但是无论是使用什么中间件也不能无休止的增加数据库链接来解决业务需求过多的问题。
|
Loading…
Add table
Add a link
Reference in a new issue