postgrep修改存儲目錄

  • 2019 年 12 月 20 日
  • 筆記

此篇文檔為轉載,來自趙熠東的csdn部落格,地址暫時未找到

安裝yum源

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

安裝客戶端和服務端

yum install -y postgresql10-server postgresql10

安裝完會在系統中創建postgres用戶,並在其.bash_profile中設置PGDATA=/var/lib/pgsql/10/data

在/usr/lib/systemd/system/目錄創建postgresql-10.service用於支援 systemd調用

systemd設置開機啟動原理

支援 systemd啟動的程式會在/usr/lib/systemd/system/下建立.service啟動腳本

systemctl enable postgresql-10.service

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-10.service to /usr/lib/systemd/system/postgresql-10.service.

設置開機啟動就是在/etc/systemd/system/multi-user.target.wants/設置/usr/lib/systemd/system/對應service的符號鏈接

systemctl disable postgresql-10.service

Removed symlink /etc/systemd/system/multi-user.target.wants/postgresql-10.service

取消開機啟動就是在/etc/systemd/system/multi-user.target.wants/刪除對應service的符號鏈接

資料庫初始化腳本postgresql-10-setup會讀取/usr/lib/systemd/system/postgresql-10.service腳本裡面的PGDATA用來設置資料庫文件的存放位置

創建資料庫數據文件存放目錄

mkdir -p /data/pgsql/10/data/

chown postgres /data/pgsql -R

修改配置文件

使用root用戶修改/usr/lib/systemd/system/postgresql-10.service的PGDATA路徑

vim /usr/lib/systemd/system/postgresql-10.service

Environment=PGDATA=/var/lib/pgsql/10/data/

改為

Environment=PGDATA=/data/pgsql/10/data/

然後

systemctl daemon-reload

重新載入配置文件

修改postgres用戶的~/.bash_profile的環境變數PGDATA為實際路徑(不修改這個對於整個安裝過程沒有任何影響)

su – postgres

vim ~/.bash_profile

PGDATA=/var/lib/pgsql/10/data

改為

PGDATA=/data/pgsql/10/data

確認配置文件

postgresql-10-setup初始化腳本,會通過

systemctl show -p Environment "postgresql-10.service" |

sed 's/^Environment=//' | tr ' ' 'n' |

sed -n 's/^PGDATA=//p' | tail -n 1

獲取資料庫文件存放的位置,執行以上命令,如果顯示的路徑跟設置的路徑不一致,就需要執行

systemctl daemon-reload

重新載入配置文件,再次查看,如果路徑還不對,就說明設置的路徑有問題。

初始化資料庫

使用root用戶執行

/usr/pgsql-10/bin/postgresql-10-setup initdb

初始化資料庫後會在/data/pgsql/10/data/創建資料庫相關的數據文件和 配置文件

並且會將資料庫文件存放的目錄/data目錄許可權設為0700,所以如果要遷移到其他路徑,也應該將該目錄設為 chmod 0700,否則啟動會報錯

開啟遠程訪問

修改配置文件postgresql.conf

vim /data/pgsql/10/data/postgresql.conf

修改#listen_addresses = 'localhost' 為 listen_addresses='*' (注意需要刪除#注釋)

當然,此處『*』也可以改為任何你想開放的伺服器IP

信任遠程連接

修改配置文件pg_hba.conf

vim /data/pgsql/10/data/pg_hba.conf

使用shift+g跳至底部

# IPv4 local connections:

host all all 127.0.0.1/32 ident

# IPv4 local connections:

host all all 0.0.0.0/0 md5

0.0.0.0/0表示所有IP可連接,也可以設置為特定IP

設置開機啟動

systemctl enable postgresql-10

啟動資料庫

systemctl start postgresql-10

修改資料庫管理員密碼

su – postgres

psql

輸入

password

或者

password postgres

防火牆開放5432埠

CentOS 防火牆中內置了PostgreSQL服務,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,我們只需以服務方式將PostgreSQL服務開放即可

firewall-cmd –add-service=postgresql –permanent 開放postgresql服務

firewall-cmd –reload 重載防火牆

參考地址

https://www.postgresql.org/download/linux/redhat/

https://www.itzgeek.com/how-tos/linux/centos-how-tos/install-postgresql-9-3-on-centos-7.html

https://tecadmin.net/install-postgresql-server-centos/

https://www.jianshu.com/p/6f9110921b59

https://www.cnblogs.com/stulzq/p/7766409.html

https://blog.csdn.net/shanzhizi/article/details/50662286

https://www.cnblogs.com/think8848/p/5877076.html

http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html