版本信息:
CentOS版本:CentOS-7-x86_64-Minimal-1810
PostgreSQL版本: PostgreSQL 10.10, 64-bit
第一部分:PostgresSQL的安裝
1、安裝rpm文件
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2、安裝客戶端
yum install postgresql10
3、安裝服務端
yum install postgresql10-server
4、初始化
/usr/pgsql-10/bin/postgresql-10-setup initdb
5、設置自動啟動並且啟動postgresql服務
systemctl enable postgresql-10
systemctl start postgresql-10
postgresql的安裝比較簡單,官網上有明確的操作步驟
第二部分:創建用戶和數據庫
1、使用postgres用戶登錄(PostgresSQL安裝后會自動創建postgres用戶,無密碼)
su - postgres
2、登錄postgresql數據庫
3、創建用戶和數據庫並授權
create user test_user with password 'abc123'; // 創建用戶
create database test_db owner test_user; // 創建數據庫
grant all privileges on database test_db to test_user; // 授權
4、退出psql(輸入 \q 再按回車鍵即可)
\q
第三部分:開啟遠程訪問
1、修改/var/lib/pgsql/10/data/postgresql.conf文件,取消 listen_addresses 的注釋,將參數值改為“*”
2、修改/var/lib/pgsql/10/data/pg_hba.conf文件,增加下圖紅框部分內容
3、切換到root用戶,重啟postgresql服務
systemctl restart postgresql-10.service
4、使用數據庫連接工具測試連接
第四部分:額外補充
1、修改默認生成的 postgres 用戶密碼(此postgres非上面的postgres用戶,此為數據庫的用戶,上面的為操作系統的用戶)
su - postgres
psql -U postgres
alter user postgres with encrypted password '1';
2、服務啟動、關閉、重啟、查看狀態命令
systemctl start postgresql-10.service // 啟動服務 systemctl stop postgresql-10.service // 關閉服務 systemctl restart postgresql-10.service // 重啟服務 systemctl status postgresql-10.service // 查看狀態