新装的pg数据库,默认只允许使用本地socket方式登录,不允许远程登录 psql: error: could not connect to server: Connection refused Is the server running on host "192.168.111.128" and accepting TCP/IP connections on port 5432?
修改配置文件,实现远程登录
vim $PGDATA/pg_hba.conf
1 2
# IPv4 local connections: host all all 192.168.111.0/24 md5
vim $PGDATA/postgresql.conf
1 2
#listen_addresses = 'localhost' # what IP address(es) to listen on; listen_addresses = '*'
postgres=# create user user1 with PASSWORD '1'; --有login权限 CREATE ROLE postgres=# create role user2 with PASSWORD '1'; --无login权限 CREATE ROLE --查看用户信息 postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} user1 | | {} user2 | Cannot login | {}
--创建一个超级用户user1 postgres=# create user user1 with SUPERUSER PASSWORD '111111'; CREATE ROLE --创建一个用户user2,允许登录,设置过期时间 postgres=# create user user2 with LOGIN PASSWORD 'user#123' VALID UNTIL '2022-09-30'; CREATE ROLE --删除用户 postgres=# drop user user1; DROP ROLE --修改用户密码 postgres=# alter user user2 with password '1'; ALTER ROLE
权限管理
权限级别
cluster权限:实例权限通过pg_hba.conf配置。
database权限: 数据库权限通过grant和revoke操作schema配置。
TBS权限:表空间权限通过grant和revoke操作表、物化视图、索引、临时表配置。
schema权限:模式权限通过grant和revoke操作模式下的对象配置。
object权限:对象权限通过grant和revoke配置
权限的定义
database权限设置
1
GRANT create ON DATABASE db1 to user1;
schema权限配置
1 2
ALTER SCHEMA abc OWNER to user1; GRANT select,insert,update,delete ON ALL TABLES IN SCHEMA abc to user1;
object权限设置
1
GRANT select,insert,update,delete ON abc.tb1 TO USER1;
案例:创建一个业务用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
postgres=# create database db1; CREATE DATABASE postgres=# \c db1; You are now connected to database "db1" as user "postgres". db1=# create schema abc; CREATE SCHEMA db1=# create user u1 with login password '111'; CREATE ROLE db1=# alter schema abc owner to u1; ALTER SCHEMA db1=# GRANT select,insert,delete,update ON ALL TABLES IN SCHEMA abc to u1; GRANT
# 设置一个用户A可以使用用户B的数据 GRANT USAGE ON SCHEMA USERB to USERA;