1.PostgreSQL的安裝
1.1 下載並安裝PostgreSQL官方yum源配置文件
dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
1.2 禁用系統內置yum源的PostgreSQL安裝模塊
PostgreSQL官方的yum源配置文件提供了PostgreSQL12/11/10/9.6/9.5共5個版本的配置信息,一般情況下我們只使用計划安裝版本的配置信息,禁用不需要的配置信息可以提高下載速度。要安裝12版,可以禁用11/10/9.6/9.5版的配置信息,以及禁用系統內置yum源的PostgreSQL安裝模塊
CentOS8的內置yum源中已經提供PostgreSQL安裝模塊(但比官方提供的版本要舊),而在執行安裝命令時,內置yum源的優先級高於其他yum源,因此要禁用內置yum源的PostgreSQL安裝模塊。
dnf module list postgresql dnf config-manager --disable pgdg11 dnf config-manager --disable pgdg10 dnf config-manager --disable pgdg96 dnf config-manager --disable pgdg95
dnf module disable postgresql
1.3 安裝PostgreSQL12的客戶端和服務器端程序
dnf install postgresql12 dnf install postgresql12-server dnf install postgresql12-contrib
注意:程序安裝目錄是"/usr/pgsql-12",程序運行目錄是"/var/run/postgresql",程序運行用戶和組是"postgres:postgres","postgres"用戶和組安裝時默認創建。
2.PostgreSQL的配置
2.1 設置數據庫實例的數據存儲目錄
數據庫實例的默認數據存儲目錄是"/var/lib/pgsql/12/data/"。"/var"是一個系統目錄,不宜存放大量業務數據。因此需要在初始化數據庫實例之前設置數據存儲目錄。
1)創建數據存儲目錄
mkdir /data/pgsql12-data
2)設置數據存儲目錄的所有者用戶和組為"postgres:postgres","postgres"用戶和組在安裝PostgreSQL12時已創建
chown postgres:postgres /data/pgsql12-data
3)修改PostgreSQL12開機啟動服務配置文件,設置為新的數據存儲目錄
vim /usr/lib/systemd/system/postgresql-12.service
修改配置文件中的"Environment"參數項並保存。
[Unit] Description=PostgreSQL 12 database server Documentation=https://www.postgresql.org/docs/12/static/ After=syslog.target After=network.target [Service] Type=notify User=postgres Group=postgres # Location of database directory #Environment=PGDATA=/var/lib/pgsql/12/data/ Environment=PGDATA=/data/pgsql12-data/ # This is normally controlled by the global default set by systemd # StandardOutput=syslog # Disable OOM kill on the postmaster OOMScoreAdjust=-1000 Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj Environment=PG_OOM_ADJUST_VALUE=0 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} ExecStart=/usr/pgsql-12/bin/postmaster -D ${PGDATA} ExecReload=/bin/kill -HUP $MAINPID KillMode=mixed KillSignal=SIGINT # Do not set any timeout value, so that systemd will not kill postmaster # during crash recovery. TimeoutSec=0 [Install] WantedBy=multi-user.target
2.2 初始化數據庫實例
進入程序安裝目錄下的"bin"目錄下,執行"postgresql-12-setup initdb"命令。
cd /usr/pgsql-12/bin ./postgresql-12-setup initdb
2.3 啟動數據庫實例服務,並設置為開機自動啟動
systemctl enable postgresql-12.service systemctl start postgresql-12.service
2.4 設置數據庫實例超級管理員賬戶"postgres"的口令
PostgreSQL12安裝完成后"postgres"的默認口令為空,為空時無法使用該用戶登錄數據庫。
passwd postgres su postgres bash-4.4$ psql psql (12.3) Type "help" for help. postgres=# alter user postgres with password 'gis'; ALTER ROLE postgres=# \q bash-4.4$ exit
2.5 設置數據庫實例的遠程訪問策略
PostgreSQL12安裝完成后默認只允許本地訪問
1)設置數據庫實例訪問策略,可以設置多個由主機類型、數據庫、用戶、IP地址組成的策略。
vim /data/pgsql12-data/pg_hba.conf
在文件的"# IPv4 local connections"策略中追加一條“允許全部用戶,通過全部網絡地址訪問全部數據庫”的策略並保存,策略定義如下:
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 ident # 追加一條“允許全部用戶,通過全部網絡地址訪問全部數據庫”的策略 host all all 0.0.0.0/0 trust # IPv6 local connections: host all all ::1/128 ident # Allow replication connections from localhost, by a user with the # replication privilege. local replication all peer host replication all 127.0.0.1/32 ident host replication all ::1/128 ident
2)設置數據庫實例監聽地址和端口
監聽地址,"*"表示全部地址,默認是"localhost"
監聽端口,默認是"5432"
vim /data/pgsql12-data/postgresql.conf
listen_addresses = '*' # defaults to 'localhost'; use '*' for all port = 5432 # (change requires restart)
3)設置防火牆端口
CentOS8默認安裝firewall防火牆,允許"5432"端口(PostgreSQL默認端口)訪問服務器。
firewall-cmd --zone=public --add-port=5432/tcp --permanent firewall-cmd --reload
2.6 重新啟動數據庫服務實例
systemctl restart postgresql-12.service
3.數據庫的運維管理
1、數據庫啟動、停止、重啟、查看狀態、開機自啟動、禁用開機自啟動
# 啟動數據庫 systemctl start postgresql-12.service # 停止數據庫 systemctl stop postgresql-12.service # 重啟數據庫 systemctl restart postgresql-12.service # 查看數據庫狀態 systemctl status postgresql-12.service # 開啟數據庫開機自啟動 systemctl enable postgresql-12.service # 禁用數據庫開機自啟動 systemctl disable postgresql-12.service
2、初始化數據庫
sudo -u postgres /usr/pgsql-12/bin/pg_ctl -D /data/pgsql12-data init
3、將從屬節點提升為主要節點
systemctl status postgresql-12.service su postgres bash-4.4$ rm -rf /data/pgsql12-data/* bash-4.4$ /usr/pgsql-12/bin/pg_ctl -D /data/pgsql12-data promote bash-4.4$ exit
4、備份全部數據庫
1)備份數據庫(包含創建數據庫)
sudo -u postgres /usr/pgsql-12/bin/pg_dump -C db_name > db_bak.sql
2)備份數據庫內容(不含創建數據庫)
sudo -u postgres /usr/pgsql-12/bin/pg_dump db_name > db_content_bak.sql
3)備份數據庫架構(命名空間/模式)和內容(包含創建數據庫架構)
sudo -u postgres /usr/pgsql-12/bin/pg_dump -n "schema_name" db_name > schema_bak.sql
4)備份表內數據(不含創建表結構)
sudo -u postgres /usr/pgsql-12/bin/pg_dump -a -t "schema_name.table_name" db_name > table_content_bak.sql
5、恢復全部數據庫
1)恢復數據庫及其內容(數據庫不存在)
sudo -u postgres /usr/pgsql-12/bin/psql -e < db_bak.sql
2)恢復數據庫內容(數據庫必須已存在,且庫中不存在備份文件中將要的創建的對象)
sudo -u postgres /usr/pgsql-12/bin/psql -e db_name < db_bak.sql
-------------------------------------------完------------------------------------------