centos8安裝postgresql 及 pgadmin4
- 安裝postgresql12
-
- 其他常用指令
- 安裝 pgadmin4
https://www.cnblogs.com/gispathfinder/p/13054284.html
安裝postgresql12
1、下載下傳并安裝PostgreSQL官方yum源配置檔案
# 安裝 rpm 倉庫
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2、禁用系統内置yum源的PostgreSQL安裝子產品
PostgreSQL官方的yum源配置檔案提供了PostgreSQL12/11/10/9.6/9.5共5個版本的配置資訊,一般情況下我們隻使用計劃安裝版本的配置資訊,禁用不需要的配置資訊可以提高下載下傳速度。要安裝12版,可以禁用11/10/9.6/9.5版的配置資訊,以及禁用系統内置yum源的PostgreSQL安裝子產品
sudo dnf module list postgresql
sudo dnf config-manager --disable pgdg11
sudo dnf config-manager --disable pgdg10
sudo dnf config-manager --disable pgdg96
sudo dnf config-manager --disable pgdg95
sudo dnf module disable postgresql
3、安裝PostgreSQL12的用戶端和伺服器端程式
sudo dnf install -y postgresql12
sudo dnf install -y postgresql12-server
sudo dnf install -y postgresql12-contrib
注意:程式安裝目錄是"/usr/pgsql-12",程式運作目錄是"/var/run/postgresql",程式運作使用者群組是"postgres:postgres","postgres"使用者群組安裝時預設建立。
4、設定資料庫執行個體的資料存儲目錄
資料庫執行個體的預設資料存儲目錄是"/var/lib/pgsql/12/data/"。"/var"是一個系統目錄,不宜存放大量業務資料。是以需要在初始化資料庫執行個體之前設定資料存儲目錄。
設
建立資料存儲目錄
sudo mkdir /data/pgsql12-data
設定資料存儲目錄的所有者使用者群組為"postgres:postgres","postgres"使用者群組在安裝PostgreSQL12時已建立
sudo chown postgres:postgres /data/pgsql12-data
修改PostgreSQL12開機啟動服務配置檔案,設定為新的資料存儲目錄
sudo vim /usr/lib/systemd/system/postgresql-12.service
修改 Environment=PGDATA=/data/pgsql12-data/
[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
5、初始化資料庫執行個體
進入程式安裝目錄下的"bin"目錄下,執行"postgresql-12-setup initdb"指令。
cd /usr/pgsql-12/bin
sudo ./postgresql-12-setup initdb
6、 啟動資料庫執行個體服務,并設定為開機自動啟動
systemctl enable postgresql-12.service
systemctl start postgresql-12.service
7、設定資料庫執行個體超級管理者賬戶"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
8、 設定資料庫執行個體的遠端通路政策
PostgreSQL12安裝完成後預設隻允許本地通路設定。
資料庫執行個體通路政策,可以設定多個由主機類型、資料庫、使用者、IP位址組成的政策。
vim /data/pgsql12-data/pg_hba.conf
9、在檔案的"# 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 md5
# 追加一條“允許全部使用者,通過全部網絡位址通路全部資料庫”的政策
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
10、設定資料庫執行個體監聽位址和端口
監聽位址,"*“表示全部位址,預設是"localhost”
監聽端口,預設是"5432"
vim /data/pgsql12-data/postgresql.conf
listen_addresses = '*' # defaults to 'localhost'; use '*' for all
port = 5432 # (change requires restart)
11、設定防火牆端口
CentOS8預設安裝firewall防火牆,允許"5432"端口(PostgreSQL預設端口)通路伺服器
firewall-cmd --zone=public --add-port=5432/tcp --permanent
firewall-cmd --reload
12、重新啟動資料庫服務執行個體
systemctl restart postgresql-12.service
其他常用指令
将從屬節點提升為主要節點
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
備份資料庫(包含建立資料庫)
sudo -u postgres /usr/pgsql-12/bin/pg_dump -C db_name > db_bak.sql
備份資料庫内容(不含建立資料庫)
sudo -u postgres /usr/pgsql-12/bin/pg_dump db_name > db_content_bak.sql
備份資料庫架構(命名空間/模式)和内容(包含建立資料庫架構
sudo -u postgres /usr/pgsql-12/bin/pg_dump -n "schema_name" db_name > schema_bak.sql
備份表内資料(不含建立表結構)
sudo -u postgres /usr/pgsql-12/bin/pg_dump -a -t "schema_name.table_name" db_name > table_content_bak.sql
恢複資料庫及其内容(資料庫不存在)
sudo -u postgres /usr/pgsql-12/bin/psql -e < db_bak.sql
恢複資料庫内容(資料庫必須已存在,且庫中不存在備份檔案中将要的建立的對象)
sudo -u postgres /usr/pgsql-12/bin/psql -e db_name < db_bak.sql
安裝 pgadmin4
https://www.daehub.com/archives/9543.html
# 安裝EPEL
sudo dnf install -y epel-release
sudo dnf --enablerepo=PowerTools install -y pgadmin4
# 開啟http
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd
pgAdmin 是一套使用 Python 3 開發的 Web 應用程式,軟體包中包含了示例配置檔案,使用如下指令啟用 pgAdmin 的示例配置檔案
sudo cp /etc/httpd/conf.d/pgadmin4.conf.sample /etc/httpd/conf.d/pgadmin4.conf
測試伺服器是否啟動成功
sudo httpd -t
建立 pgAdmin 的庫目錄以及日志目錄
sudo mkdir -p /var/lib/pgadmin4/
sudo mkdir -p /var/log/pgadmin4/
目錄建立完成後,就需要修改 pgAdmin 的配置檔案”/usr/lib/python3.6/site-packages/pgadmin4-web/config_distro.py”,指定日志檔案、SQLite資料庫檔案、會話檔案以及存儲目錄的位置,修改檔案為如下内容:
LOG_FILE = '/var/log/pgadmin4/pgadmin4.log'
SQLITE_PATH = '/var/lib/pgadmin4/pgadmin4.db'
SESSION_DB_PATH = '/var/lib/pgadmin4/sessions'
STORAGE_DIR = '/var/lib/pgadmin4/storage'
建立 pgAdmin 的管理帳戶以及配置資料庫
python3 /usr/lib/python3.6/site-packages/pgadmin4-web/setup.py
現在,修改相關目錄的屬主,以便 pgAdmin 網站可以進行讀寫操作:
sudo chown -R apache:apache /var/lib/pgadmin4
sudo chown -R apache:apache /var/log/pgadmin4
如果系統啟用了 SELinux,則使用如下指令設定通路規則
sudo chcon -t httpd_sys_rw_content_t /var/log/pgadmin4 -R
sudo chcon -t httpd_sys_rw_content_t /var/lib/pgadmin4 -R
sudo setsebool -P httpd_can_network_connect 1
如果系統啟用了 Firewalld 防火牆,則使用如下指令放行80和443端口
sudo firewall-cmd --permanent --zone public --add-port 80/tcp
sudo firewall-cmd --permanent --zone public --add-port 443/tcp
sudo firewall-cmd --reload
最後,重新啟動 Apache 服務,讓所有設定生效
sudo systemctl restart httpd
網站啟動成功後,就可以通過浏覽器通路如下位址進入管理界面:
http://SERVER_IP/pgadmin4
OR
http://localhost/pgadmin4