一、基礎信息說明
官網:https://www.postgresql.org/
下載說明地址:https://www.postgresql.org/download/linux/redhat/
中文社區 http://www.postgres.cn/index.php/v2/home
中文網 https://postgres.fun/
易百教程 https://www.yiibai.com/postgresql
二、系統、工具說明
1、系統版本 Centos7.4 CentOS-7-x86_64-Minimal-1804
下載地址: http://archive.kernel.org/centos-vault/7.4.1708/isos/x86_64/
2、VMware 版本:VMware Workstation Pro15
虛擬機安裝過程可參考:https://blog.csdn.net/llwy1428/article/details/89328381
3、工具:xshell5
三、安裝、部署
1、配置虛擬機網絡,每臺虛擬機均接入互聯網
參考:
https://blog.csdn.net/llwy1428/article/details/85058028
2、設置靜態IP
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
Network 重啓
[root@localhost ~]# service network restart
3、安裝基本工具
[root@localhost ~]# yum install -y vim lrzsz tree wget rpm net-tools
[root@localhost ~]# yum update -y
4、瀏覽器打開 https://www.postgresql.org/download/linux/redhat/ (官網教程)
如下圖:
5、安裝資源 rpm 、客戶端、服務端
[root@localhost ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
[root@localhost ~]# yum install -y postgresql11 (客戶端安裝)
[root@localhost ~]# yum install -y postgresql11-server (服務端安裝)
6、數據庫初始化(指定目錄)
創建目錄、增加用戶、給用戶賦權
創建目錄
[root@localhost ~]# mkdir /data
[root@localhost ~]# mkdir /data/pgdata
創建用戶
[root@localhost ~]# useradd postgres
給創建的目錄賦予 postgres 用戶權限
[root@localhost ~]# chown postgres:root /data/pgdata/
7、初始化數據庫
切換用戶
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ /usr/pgsql-11/bin/initdb -D /data/pgdata/
8、查看初始化後的數據目錄
[root@localhost ~]# su - postgres (切換用戶)
[postgres@localhost ~]$ ll /data/pgdata/
9、修改配置文件
root用戶下操作,將其中的 PGDATA 修改爲新的數據目錄:
[root@localhost ~]$ vim /usr/lib/systemd/system/postgresql-11.service
10、切換用戶 postgres 修改配置文件
[root@localhost ~]# su - postgres (切換用戶)
[postgres@localhost ~]$ vim /data/pgdata/postgresql.conf
[root@localhost ~]# su - postgres (切換用戶)
[postgres@localhost ~]$ vim /data/pgdata/pg_hba.conf
host all all 0.0.0.0/0 md5
說明:
TYPE:pg的連接方式,local:本地unix套接字,host:tcp/ip連接
DATABASE:指定數據庫
USER:指定數據庫用戶
ADDRESS:ip地址,可以定義某臺主機或某個網段,32代表檢查整個ip地址,相當於固定的ip,24代表只檢查前三位,最後一 位是0~255之間的任何一個
METHOD:認證方式,常用的有ident,md5,password,trust,reject。
md5是常用的密碼認證方式。
password是以明文密碼傳送給數據庫,建議不要在生產環境中使用。
trust是隻要知道數據庫用戶名就能登錄,建議不要在生產環境中使用。
reject是拒絕認證。
11、root 下,防火牆開啓 postgresql 數據庫服務的端口(如條件允許,可直接關閉防火牆)
[root@localhost ~]# firewall-cmd --add-port=5432/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
其它防火牆操作可參考:
https://blog.csdn.net/llwy1428/article/details/99676257
12、數據庫服務器開啓、重啓和狀態查看
啓動服務:
[root@localhost ~]# systemctl start postgresql-11
停止服務:
[root@localhost ~]# systemctl stop postgresql-11
重啓服務:
[root@localhost ~]# systemctl restart postgresql-11
13、查看啓動狀態
[root@localhost ~]# netstat -lntp
14、設置數據庫密碼(此處我設置的密碼是: 123456 )
切換用戶
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ psql
psql (11.5)
Type "help" for help.
postgres=# ALTER USER postgres ENCRYPTED PASSWORD '123456';
ALTER ROLE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
15、驗證數據庫的使用狀態
記一次 Centos7 安裝 PostgreSql 數據庫-指定數據存放地址操作完畢!
安裝、配置 Pgadmin 可參考:
https://hunter.blog.csdn.net/article/details/102486511
擴展:Centos7 編譯安裝 PostgreSql 11.4
https://blog.csdn.net/llwy1428/article/details/95444151
PostgreSql 基本操作
https://blog.csdn.net/llwy1428/article/details/102598732