PostgreSQL 安装 用户配置
一、為什么選擇 PostgreSQL
自從MySQL被Oracle收購以后,PostgreSQL逐漸成為開源關系型數據庫的首選。
MySQL被oracle收購,innodb隨之被oracle控制。
二、安裝
1、MacOS
最方便的方法是安裝 PostgreSQL.app。
2、Linux (CentOS 7)
(1)安裝
https://tecadmin.net/install-postgresql-server-centos/
(2)啟動
systemctl start postgresql-10.service systemctl enable postgresql-10.service(3)本地訪問
先登錄 shell (以 postgres 用戶為例):
sudo -i -u postgresorsudo su - postgres然后,輸入:
# 以當前登錄的 linux 用戶名為數據庫名 (這點跟 mysql 不一樣,必須得先指定登錄的數據庫) psql # 手動指定數據庫名 psql -d postgres想退出:
\q解釋:
1、安裝成功后,PostgreSQL 會自動創建一個默認用戶(且是最高權限 Superuser),名稱為 postgres,密碼為空。但是不支持遠程登錄(報錯:psql: fe_sendauth: no password supplied),必須設置密碼后才行(普通用戶也適應這個規則)。
PostgreSQL 早期名稱叫 postgres,后來在某個版本更新后就改名成了現在的 PostgreSQL。
2、PostgreSQL 每創建一個新用戶,都會生成一個新的對應的 linux 同名用戶,且登錄此用戶的 shell ,去執行psql 無需密碼 ( 本質上是因為 pg_hba.conf 的配置 )
PostgreSQL 這種通過將 Linux 用戶與PostgreSQL 帳戶相關聯來處理身份驗證的方式,被稱為“對等”身份驗證。
(4)開啟遠程訪問
方法一:通過 ssh
1、修改pg_hba.conf
sudo vi /var/lib/pgsql/data/pg_hba.conf然后把文件拉到底,最后一段里,將“ident”替換為“md5”,最終是這樣的:
# TYPE DATABASE USER ADDRESS METHOD# "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5 # Allow replication connections from localhost, by a user with the # replication privilege. local replication postgres peer host replication postgres 127.0.0.1/32 md5 host replication postgres ::1/128 md5若不知道 PostgreSQL 的配置文件路徑,可在 PostgreSQL CLI 里輸入:SHOW config_file ;
另一個重要的配置文件是/var/lib/pgsql/data/postgresql.conf
2、連接
psql -U postgres -d postgres -h xxx.xxx.xxx.xxx -W
方法二:通過 SSL 證書
略 (使用PostgreSQL設置SSL)
(5)當前登錄用戶
查看:
select current_user;
切換:
\c next_user;
(6)當前數據庫
查看:
select current_database();
切換:
\c - next_db;
三、角色管理
從版本8.1開始,PostgreSQL使用角色概念來合并用戶和組概念。
1、默認用戶
PostgreSQL 默認會有一個 Linux user 和 PostgreSQL user 都叫 postgres。這點上面有詳細提到,這里不再贅述。
除了訪問數據庫軟件之外,不要將 Linux 的 ”postgres“ 用戶用于其他任何用戶。這是一個重要的安全考慮因素。
2、管理用戶
(1)查看所有用戶
\du
List of rolesRole name | Attributes | Member of----------------------+------------------------------------------------+--------------------------------------- -----------------------pg_monitor | Cannot login | {pg_read_all_settings,pg_read_all_stat s,pg_stat_scan_tables}pg_read_all_settings | Cannot login | {}pg_read_all_stats | Cannot login | {}pg_signal_backend | Cannot login | {}pg_stat_scan_tables | Cannot login | {}postgres | Superuser, Create role, Create DB, Replication | {}(2)創建新用戶
建議:為每個應用程序創建單獨的角色(Linux user + PostgreSQL user)。
CREATE ROLE role_name;
CREATE USER role_name; 可以默認給用戶加上 LOGIN 權限。
角色創建時定義權限:
CREATE ROLE role_name WITH LOGIN;
角色權限的詳細介紹看下面。
(3)刪除用戶
DROP ROLE role_name;
DROP ROLE IF EXISTS role_name; (不報錯寫法)
(4)用戶權限
查看所有的權限種類:
\h CREATE ROLE
Command: CREATE ROLE Description: define a new database role Syntax: CREATE ROLE name [ [ WITH ] option [ ... ] ]where option can be:SUPERUSER | NOSUPERUSER| CREATEDB | NOCREATEDB| CREATEROLE | NOCREATEROLE| CREATEUSER | NOCREATEUSER| INHERIT | NOINHERIT| LOGIN | NOLOGIN| REPLICATION | NOREPLICATION| CONNECTION LIMIT connlimit| [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'| VALID UNTIL 'timestamp'| IN ROLE role_name [, ...]| IN GROUP role_name [, ...]| ROLE role_name [, ...]| ADMIN role_name [, ...]| USER role_name [, ...]| SYSID uid更改用戶權限:
ALTER ROLE role_name WITH attribute_options;
PASSWORD 也算權限的一種:ALTER ROLE role_name WITH PASSWORD 'xxxx';
3、數據操作 & owner - 所有者權限
(1) 數據庫層級
PostgreSQL 的層級還是蠻深的,database -> schema -> object(table)。
而 mysql 忽略了schema這個層級,就很方便。
(2) 數據庫
創建:
CREATE DATABASE test_db;
CREATE DATABASE test_db OWNER role_name; (創建時指定數據庫 owner)
刪除:
DROP DATABASE test_db;
更改 owner:
ALTER DATABASE test_db owner to role_name;
查看 & owner 權限:
\l
List of databasesName | Owner | Encoding | Collate | Ctype | Access privileges --------------------+----------+-----------+-------------+-------+-----------------------postgres | postgres | SQL_ASCII | en_US.UTF-8 | C |space_production | space | SQL_ASCII | en_US.UTF-8 | C | =Tc/space +| | | | | space=CTc/spacetemplate0 | postgres | SQL_ASCII | en_US.UTF-8 | C | =c/postgres +| | | | | postgres=CTc/postgrestemplate1 | postgres | SQL_ASCII | en_US.UTF-8 | C | =c/postgres +| | | | | postgres=CTc/postgres(3) Schema
創建:
略
刪除:
略
更改 owner:
ALTER SCHEMA "public" OWNER TO role_name;
查看 & owner 權限:
\dnS
List of schemasName | Owner --------------------+----------information_schema | postgrespg_catalog | postgrespg_temp_1 | postgrespg_temp_3 | postgrespg_temp_4 | postgrespg_toast | postgrespg_toast_temp_1 | postgrespg_toast_temp_3 | postgrespg_toast_temp_4 | postgrespublic | test(4) 數據表
創建:
略
刪除:
略
更改 owner:
ALTER TABLE test_table OWNER to role_name;
查看:
\d
List of relationsSchema | Name | Type | Owner --------+--------------------------------+----------+---------public | Admins | table | testpublic | Admins_id_seq | sequence | test4、 訪問權限 - Access privileges
初始狀態只有 owner(或超級用戶) 有所有權限。但要允許其他角色擁有權限(或部分權限),必須授予。
(1)添加
GRANT permission_type on test_table to role_name;
permission_type : ALL、CRUD(如UPDATE) 、CONNECT……
role_name:可以替換成 PUBLIC 代表所有用戶,或者 group 用戶。
(2)查看
\z
Access privilegesSchema | Name | Type | Access privileges | Column access privileges --------+--------------------------------+----------+-------------------+--------------------------public | Admins | table | |public | Admins_id_seq | sequence | |?
(3)刪除
用 REVOKE 替換上面的 GRANT
5、組 & 子角色
(1) 先創建組角色
CREATE ROLE temporary_users;(2) 再創建組角色的成員
GRANT temporary_users TO test_user_1; GRANT temporary_users TO test_user_2;于是,test_user_1 和 test_user_2 這兩個用戶可以通過操縱“temporary_users”組角色來管理其權限,而不是單獨管理每個成員。很方便。
查看組角色情況:
\du
List of rolesRole name | Attributes | Member of -----------------+------------------------------------------------+-------------------test_user_1 | | {temporary_users}postgres | Superuser, Create role, Create DB, Replication | {}temporary_users | Cannot login | {}test_user_2 | | {temporary_users}(3) 組角色成員 獲取 組角色 的權限
方法一:set role
SET ROLE temporary_users; RESET ROLE;如果是“postgres”用戶(即超級用戶),即便我們不是該組的成員,我們也可以使用“set role”。
方法二: 用 alter role ,避免以后每次都要 set role
ALTER ROLE test_user_2 INHERIT;(4) 刪除組角色
就跟刪除普通的用戶一樣:
DROP ROLE temporary_users;如果此用戶擁有一些數據,請先轉移 owner,再刪除用戶,不然會失敗報錯。
刪除組成員不會把屬于組成員的成員刪除。
參考資料
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-centos-7
轉載于:https://www.cnblogs.com/xjnotxj/p/11198255.html
總結
以上是生活随笔為你收集整理的PostgreSQL 安装 用户配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重返帝国云游商人在哪?
- 下一篇: 提高SQL执行性能方案:如何让你的SQL