1. 先需要下載相應的rpm包
地址
https://pkgs.org/search/?q=postgresql12
一般至少要下載如下四個包
postgresql12-12.3-1PGDG.rhel7.x86_64.rpm postgresql12-contrib-12.3-1PGDG.rhel7.x86_64.rpm postgresql12-libs-12.3-1PGDG.rhel7.x86_64.rpm postgresql12-server-12.3-1PGDG.rhel7.x86_64.rpm
#注意 PGDG 應該是安裝資源庫的 可以不安裝
#contrib 是安裝擴展的 沒有這個包就沒有 ossp-uuid的插件了
#server 是數據庫的安裝文件
#libs 用來客戶端進行連接.
#注意 如果是centos8 的話 選擇 rhel8 進行下載就可以了.
根據 搜索下來安裝即可
注意現在最新版本是 12.3
2. 進入到linux內的下載目錄執行
yum localinstall *.rpm
不需要聯網就可以安裝成功:
3. 查看數據庫服務
注意安裝完pg12數據庫之后會自動創建一個服務,可以看一下服務的狀態. 建完庫可能是無法使用的.
[root@CentOS76 PG12]# systemctl status postgresql-12 ● postgresql-12.service - PostgreSQL 12 database server Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Tue 2020-05-26 13:15:00 CST; 4min 18s ago Docs: https://www.postgresql.org/docs/12/static/ Process: 7355 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=1/FAILURE) May 26 13:15:00 CentOS76 systemd[1]: Starting PostgreSQL 12 database server... May 26 13:15:00 CentOS76 systemd[1]: postgresql-12.service: control process exited, code=exited status=1 May 26 13:15:00 CentOS76 systemd[1]: Failed to start PostgreSQL 12 database server. May 26 13:15:00 CentOS76 systemd[1]: Unit postgresql-12.service entered failed state. May 26 13:15:00 CentOS76 systemd[1]: postgresql-12.service failed.
查看一下上面的 service 文件就可以執行創建庫的操作了
注意這個里面的配置信息為:
[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 # Note: avoid inserting whitespace in these Environment= lines, or you may # break postgresql-setup. # Location of database directory Environment=PGDATA=/var/lib/pgsql/12/data/ # Where to send early-startup messages from the server (before the logging # options of postgresql.conf take effect) # 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
注意有兩個目錄 一個是數據目錄, 一個是程序目錄
記住這兩個目錄就可以進行其他操作了.
4. 初始數據庫
切換到PG的用戶
su - postgres
切換到 PG 的默認程序目錄
cd /usr/pgsql-12/bin/
執行初始化數據庫的腳本
./initdb -D /var/lib/pgsql/12/data/
5. 然后退回到 root 用戶 打開服務,並且設置服務自動啟動操作.
systemctl enable postgresql-12 && systemctl restart postgresql-12
6.修改數據庫密碼.
su - postgres 進入postgresql 數據庫 執行命令 psql 修改管理員的密碼: alter role postgres with password 'Test1127?!';
7. 修改連接池大小以及設置其他機器可以遠程訪問.
修改默認配置文件 vim /var/lib/pgsql/12/data/pg_hba.conf 在客戶端安全配置的地方增加一行. host all all 0.0.0.0/0 md5
# 注意 trust 就是默認免密, md5 是默認加密.
修改數據庫其他配置信息
vim /var/lib/pgsql/12/data/postgresql.conf
# 監聽所有地址的請求 注意測試環境可以,開發環境需要與防火牆一同設置, 將listen_addresses 后面的localhost 修改為 * 並且去掉前面的注釋.
listen_addresses = '*'
注意也需要同步將port 前面的注釋去掉.
# 修改 連接數 可以將max_connections的數值改大.
max_connections = 1000
8. 重啟服務就可以使用了.
systemctl restart postgresql-12