系統資源限制設置
vi /etc/security/limits.conf * soft nofile 1024000 * hard nofile 1024000 * soft noproc 1024000 * hard noproc 1024000 * soft core 1024000 * hard core 1024000 * soft memlock 1024000 * hard memlock 1024000
vi /etc/security/limits.d/20-nproc.conf * soft nproc unlimited root soft nproc unlimited
SELINUX和防火牆設置
vi /etc/sysconfig/selinux SELINUX=disabled SELINUXTYPE=targeted
OS防火牆(建議按業務場景設置,不需要就先刪除所有策略,並關閉服務)
方法1
#設置默認接受所有請求 /sbin/iptables -P INPUT ACCEPT #清除防火牆規則 /sbin/iptables -F
方法2
關閉防火牆
systemctl disable firewalld.service
systemctl stop firewalld.service
創建組及用戶,並設置密碼
groupadd postgres useradd -g postgres -m postgres passwd postgres
源碼包下載
https://www.postgresql.org/ftp/source/v12.2/ postgresql-12.2.tar.gz
檢查依賴包
rpm -q openssl \ openssl-devel \ pam \ pam-devel \ libxml2 \ libxml2-devel \ libxslt \ libxslt-devel \ perl \ perl-devel \ python-devel \ perl-ExtUtils-Embed \ readline \ readline-devel \ zlib \ zlib-devel \ gettext \ gettext-devel \ bison \ flex \ uuid-devel \ gcc \ gcc-c++ \
systemd
配置yum源參考
https://www.cnblogs.com/connected/p/12642029.html
安裝依賴包
yum install -y openssl \ openssl-devel \ pam \ pam-devel \ libxml2 \ libxml2-devel \ libxslt \ libxslt-devel \ perl \ perl-devel \ python-devel \ perl-ExtUtils-Embed \ readline \ readline-devel \ zlib \ zlib-devel \ gettext \ gettext-devel \ bison \ flex \ uuid-devel \ gcc \ gcc-c++ \
systemd*
上傳源碼包到/pg-rpm
[root@centos7-6 ~]# mkdir /pg-rpm
解壓並設置權限
cd /pg-rpm/ tar -zxvf postgresql-12.2.tar.gz chown -R postgres:postgres postgresql-12.2
創建pg安裝目錄
mkdir -p /opt/pg12 chown postgres:postgres /opt/pg12
切換用戶
su - postgres
編譯安裝
cd /pg-rpm/postgresql-12.2 ./configure \ --prefix=/opt/pg12 \ --with-pgport=9527 \ --with-openssl \ --with-perl \ --with-python \ --with-blocksize=16 \
--with-systemd
如果加上“--with-systemd”后報錯如下:
configure: error: header file <systemd/sd-daemon.h> is required for systemd support
則需要檢查是否安裝了systemd相關包,在這次驗證中缺少的是 systemd-devel-219-62.el7.x86_64
參數詳解
--------------- ./configure --prefix=PREFIX 把所有文件裝在目錄PREFIX中而不是/usr/local/pgsql中。 實際的文件會安裝到數個子目錄中;沒有一個文件會直接安裝到PREFIX根目錄里。 結果如下: [postgres@centos7-6 ~]$ ls /opt/pg12/ bin include lib share [postgres@centos7-6 ~]$ --with-pgport=NUMBER 把NUMBER設置為服務器和客戶端的默認端口。默認是 5432。 這個端口可以在以后修改,不過如果你在這里聲明,那么服務器和客戶端將有相同的編譯好了的默認值。這樣會非常方便些。 通常選取一個非默認值的理由是你企圖在同一台機器上運行多個PostgreSQL服務器。 --with-openssl 編譯SSL(加密)連接支持。這個選項需要安裝OpenSSL包。configure將會檢查所需的頭文件和庫以確保你的 OpenSSL安裝足以讓配置繼續下去。 --with-perl 制作PL/Perl服務器端編程語言。 --with-python 制作PL/Python服務器端編程語言。 --with-blocksize=BLOCKSIZE 設置塊尺寸,以 K 字節計。這是表內存儲和I/O的單位。默認值(8K字節)適合於大多數情況,但是在特殊情況下可能其他值更有用。這個值必須是2的冪並且在 1 和 32 (K字節)之間。注意修改這個值需要一次 initdb。
--with-systemd
系統服務方式管理
---------------
編譯
make world
執行完成后顯示的最后一行應該是:
PostgreSQL, contrib, and documentation successfully made. Ready to install.
注: 源碼安裝postgresql時,而make時又沒有make world,就會導致的pg最終沒有類似pg_stat_statements的擴展功能 如果選擇make ,后續手動安裝pg擴展插件,請參考https://www.cnblogs.com/ejQiu-123/p/11497362.html make (一定要記得用GNU make)。依你的硬件而異,編譯過程可能需要 5 分鍾到半小時。顯示的最后一行應該是: 如果你希望編譯所有能編譯的東西,包括文檔(HTML和手冊頁)以及附加模塊(contrib),這樣鍵入: make world
安裝PostgreSQL
make install-world
執行完成后顯示的最后一行應該是:
PostgreSQL, contrib, and documentation installation complete.
查看安裝后的目錄結構
[postgres@centos7-6 ~]$ ls /opt/pg12/ bin include lib share [postgres@centos7-6 ~]$
創建數據存放目錄並設置權限
[postgres@centos7-6 ~]$ mkdir /opt/pg12/data
[postgres@centos7-6 ~]$ chown postgres:postgres /opt/pg12/data
配置環境變量
[postgres@centos7-6 ~]$ vi ~/.bash_profile export PGHOME=/opt/pg12 export PGPORT=9527 export PGDATA=/opt/pg12/data export LD_LIBRARY_PATH=$PGHOME/lib:LD_LIBRARY_PATH export PATH=$PGHOME/bin:$PATH export PATH=/usr/local/pgsql/bin:$PATH:.
生效環境變量
source ~/.bash_profile
創建一個數據庫集簇
[postgres@centos7-6 ~]$ initdb -D /opt/pg12/data -E UTF8 --locale=zh_CN.utf8 The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "zh_CN.utf8". initdb: could not find suitable text search configuration for locale "zh_CN.utf8" The default text search configuration will be set to "simple". Data page checksums are disabled. fixing permissions on existing directory /opt/pg12/data ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... posix selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default time zone ... PRC creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok initdb: warning: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: pg_ctl -D /opt/pg12/data -l logfile start [postgres@centos7-6 ~]$
啟動
[postgres@centos7-6 ~]$ pg_ctl start waiting for server to start....2020-04-13 16:44:34.479 CST [40486] LOG: starting PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit 2020-04-13 16:44:34.485 CST [40486] LOG: listening on IPv6 address "::1", port 9527 2020-04-13 16:44:34.485 CST [40486] LOG: listening on IPv4 address "127.0.0.1", port 9527 2020-04-13 16:44:34.486 CST [40486] LOG: listening on Unix socket "/tmp/.s.PGSQL.9527" 2020-04-13 16:44:34.499 CST [40487] LOG: database system was shut down at 2020-04-13 16:42:37 CST 2020-04-13 16:44:34.503 CST [40486] LOG: database system is ready to accept connections done server started [postgres@centos7-6 ~]$
驗證安裝情況
[postgres@centos7-6 ~]$ psql -Upostgres -l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | zh_CN.utf8 | zh_CN.utf8 | template0 | postgres | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)
使用systemctl來管理postgres的服務了
vi /etc/systemd/system/postgresql.service [Unit] Description=PostgreSQL database server Documentation=man:postgres(1) [Service] Type=notify User=postgres ExecStart=/opt/pg12/bin/postgres -D /opt/pg12/data ExecReload=/bin/kill -HUP $MAINPID KillMode=mixed KillSignal=SIGINT TimeoutSec=0 [Install] WantedBy=multi-user.target
使用systemctl來管理postgres的服務
注:使用systemctl,就需要編譯安裝時加上參數:--with-systemd
systemctl start postgresql.service
systemctl status postgresql.service
systemctl stop postgresql.service
systemctl disable postgresql.service
systemctl enable postgresql.service