1. 程式人生 > 實用技巧 >安裝 部署 postgresql資料庫 搭建主從節點 (業務庫)

安裝 部署 postgresql資料庫 搭建主從節點 (業務庫)

此文轉載自:https://blog.csdn.net/yang_z_1/article/details/112259081

安裝 部署 postgresql資料庫 搭建主從節點

(時序數搭建 (待更新))

文章目錄


作業系統
64位CentOS 7
資料庫搭建

一 業務資料庫搭建

1. 安裝 yum源(伺服器可訪問網際網路時用)

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2. 安裝客戶端

yum install postgresql11 –y

3. 安裝服務端

yum install postgresql11-server -y

4. 初始化資料庫

使用yum安裝後,會在系統中建立一個postgres的無密碼使用者。在主節點伺服器切換到此使用者,然後建立一個數據目錄,該目錄將存放所有的配置檔案、資料庫檔案和日誌檔案。

root # mkdir -p /u01/apps/pgsql/data
root # chown -R  postgres:postgres /u01/apps/pgsql/

PostgreSQL的安裝目錄預設存放在/usr/pgsql-11,使用下面的命令初始化資料庫目錄:
使用postgres使用者執行:錄
我放在

/usr/pgsql-11/bin/initdb -D 【資料目錄全路徑】

5. 修改服務啟動指令碼

使用root使用者修改 /usr/lib/systemd/system/postgresql-11.service

檔案,將其中的PGDATA修改為新的資料目錄

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

6. 修改資料庫配置

使用postgres使用者進入自行建立的資料目錄,修改 postgresql.conf 檔案:
必須要修改listen_addresses、port、max_connections、log_timezone、timezone、datestyle



修改資料目錄下的 pg_hda.conf 檔案,新增允許連線到本資料庫的地址:

7. 開啟防火牆埠

firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload

8. 啟動資料庫服務

啟動服務:systemctl start postgresql-11
停止服務:systemctl stop postgresql-11
重啟服務:systemctl restart postgresql-11

9. 修改資料庫postgres使用者密碼

修改資料庫postgres使用者密碼,使用系統的postgres使用者進入psql,執行以下命令:

[root]# su - postgres
上一次登入:二 12月 29 13:35:56 CST 2020pts/0 上
-bash-4.2$ psql
psql (11.5)
輸入 "help" 來獲取幫助資訊.
 postgres=# ALTER USER postgres ENCRYPTED PASSWORD '【密碼】';

**

二 主從熱備配置

**

1. 建立熱備使用者

啟動資料庫服務,切換至作業系統的postgres使用者,執行psql
在psql命令列中執行下面的命令建立熱備使用者,注意使用者名稱只能是小寫字母:

CREATE USER 【使用者名稱】 REPLICATION LOGIN CONNECTION LIMIT 3 ENCRYPTED PASSWORD '【密碼】';

2. 進入自行建立的資料目錄,修改 postgresql.conf 檔案:

除 之前 所做的修改外,還需要修改wal_level、max_wal_senders、wal_keep_segments、hot_standby:



3. 修改資料目錄下的 pg_hda.conf 檔案,需要新增replication的連線配置,注意此處應限制為特定機器的連線,該配置的第三列配置為之前建立的熱備使用者

4. 重啟主伺服器的資料庫服務

在從伺服器上安裝PostgreSQL,僅需要建立資料目錄,不需要初始化資料庫,使用下面的命令將主服務的資料檔案同步至從伺服器:

/usr/pgsql-11/bin/pg_basebackup -h 【主伺服器地址】 -p 【主伺服器埠】 -D 【從伺服器資料目錄】 -P -U 【主伺服器熱備使用者】

在從伺服器上修改postgresql.conf,修改port為從機的埠(5433)
在從伺服器的資料目錄下建立一個recovery.conf檔案,內容如下:

standby_mode          = 'on'
primary_conninfo      = 'host=【主伺服器地址】 port=5432 user=【主伺服器熱備使用者】 password=【主伺服器熱備使用者密碼】'
trigger_file = '/tmp/pgsql_master'

啟動從伺服器的資料庫服務

5. 在主伺服器和從伺服器上檢查執行是否正常

主伺服器:

從伺服器:

使用postgres建立主伺服器,在psql下執行下列命令:
postgres=# \x on
postgres=# select * from pg_stat_activity where usename = ‘【熱備使用者名稱】’;
若顯示以下內容,說明已經成功搭建:


**

三 安裝外掛

**
安裝所需的外掛

  1. 安裝PostGIS和pgAgent
yum install postgis25_11 -y
yum install pgagent_11 -y
  1. 切換到postgres使用者,執行psql進入postgresql客戶端,在postgres資料庫中執行以下命令:
Create extension pgagent;
  1. 在postgresql客戶端中執行以下命令建立資料庫cs2:
Create database cs2;
  1. 在cs2資料庫中建立資料庫擴充套件
Create extension postgis;
Create extension "uuid-ossp";
Create extension "postgres_fdw"
  1. 使用root使用者啟動pgagent服務:
systemctl start pgagent_11
systemctl enable pgagent_11

主從的postgresql資料庫配置到這裡就結束了 下次再說 postgresql資料庫轉換成 timescaledb 時序庫