PostgreSQL安裝及介紹

安裝環境:
Red Hat Enterprise Linux Server release 7.5 (Maipo)
postgresql-11.5

截止目前PG版本12 beta 4已發佈,目前最新穩定版本未pg 11.5,發佈規律一般一季度一個小版本發佈,一年一個大版本發佈。由於安裝平臺是Linux,相關版本及介紹均以Linux爲主,一般通用開源軟件在Linux上會提供三種方式安裝二進制、RPM及源碼安裝。

PostgreSQL同樣提供了三種安裝方式,不過二進制安裝在PG 10.10以後不再提供安裝方式。

二進制下載地址:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

其他安裝方式下載地址:https://www.postgresql.org/download/

操作系統環境配置:

1)內核參數

內核參數可以參考digoal的github的參數標準文檔,其中涉及共享內存的參數需要根據實際情況調整,並且該標準中涉及大頁配置已註釋,需要根據實際情況修改

https://github.com/Gavin404/blog/blob/master/201611/20161121_01.md

2)系統資源調整,調整limits.conf後,在/etc/security/limits.d/目錄下對應文件也需要修改。

3)關閉防火牆、透明大頁、CTL+ALT+DEL重啓服務器限制,詳見以下連接

https://blog.csdn.net/Dream19881003/article/details/101210600

 

1、RPM包安裝

RPM再linux上爲最簡單安裝方式,通過yum將PostgreSQL及相關軟件包會自動安裝完成,並且註冊爲系統服務。PostgreSQL通過pgdg-redhat-repo-latest.noarch.rpm軟件包生產pgdg-redhat-all.repo資料庫,包括近期幾個大版本軟訪問連接。可以通過聯網進行在線安裝。同樣可以通過該連接選擇下載到本地拷貝到服務器上執行。

安裝repo

[root@slave01 soft]# rpm -ivh pgdg-redhat-repo-latest.noarch.rpm 
警告:pgdg-redhat-repo-latest.noarch.rpm: 頭V4 DSA/SHA1 Signature, 密鑰 ID 442df0f8: NOKEY
準備中...                          ################################# [100%]
正在升級/安裝...
   1:pgdg-redhat-repo-42.0-4          ################################# [100%]

pgdg-redhat-all.repo內容樣例:
 

[root@slave01 yum.repos.d]# more pgdg-redhat-all.repo 

# PGDG Red Hat Enterprise Linux / CentOS / Scientific Linux stable repositories:

[pgdg12]
name=PostgreSQL 12 $releasever - $basearch
baseurl=https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-$releasever-$basearch
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG

......

同樣官網上提供了安裝詳細過程,以RHEL 7爲例:

https://www.postgresql.org/download/linux/redhat/

安裝PostgreSQL11客戶端:

[root@slave01 yum.repos.d]# yum install postgresql11

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
pgdg10                                                                                    | 3.6 kB  00:00:00     
pgdg11                                                                                    | 3.6 kB  00:00:00     
pgdg94                                                                                    | 3.6 kB  00:00:00     
pgdg95                                                                                    | 3.6 kB  00:00:00     
pgdg96                                                                                    | 3.6 kB  00:00:00     
(1/10): pgdg94/7Server/x86_64/group_gz                                                    |  247 B  00:00:02     
(2/10): pgdg11/7Server/x86_64/group_gz                                                    |  245 B  00:00:02     
(3/10): pgdg95/7Server/x86_64/group_gz                                                    |  249 B  00:00:00     
(4/10): pgdg11/7Server/x86_64/primary_db                                                  | 308 kB  00:00:02     
(5/10): pgdg96/7Server/x86_64/group_gz                                                    |  249 B  00:00:00     

......

Installed:
  postgresql11.x86_64 0:11.5-1PGDG.rhel7                                                                         

Dependency Installed:
  postgresql11-libs.x86_64 0:11.5-1PGDG.rhel7                                                                    

Complete!

安裝PostgreSQL11服務器

[root@slave01 yum.repos.d]# yum install postgresql11-server

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package postgresql11-server.x86_64 0:11.5-1PGDG.rhel7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================
 Package                          Arch                Version                          Repository           Size
=================================================================================================================
Installing:
 postgresql11-server              x86_64              11.5-1PGDG.rhel7                 pgdg11              4.7 M
......

Complete!

安裝完成後會自動創建postgres用戶

[root@rhel75 soft]# cat /etc/passwd | tail -1
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

初始化數據庫及配置開啓自動啓動

[root@slave01 yum.repos.d]#/usr/pgsql-11/bin/postgresql-11-setup initdb

Initializing database ... OK
[root@slave01 yum.repos.d]#systemctl enable postgresql-11

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-11.service to /usr/lib/systemd/system/postgresql-11.service.
[root@slave01 yum.repos.d]#systemctl start postgresql-11

[root@slave01 yum.repos.d]# ps -axf | grep postgre
 12999 pts/1    S+     0:03  |   |   \_ /usr/bin/python /usr/bin/yum install postgresql11-server
 13774 pts/3    S+     0:00      |   \_ grep --color=auto postgre
 13765 ?        Ss     0:00  \_ postgres: logger   
 13767 ?        Ss     0:00  \_ postgres: checkpointer   
 13768 ?        Ss     0:00  \_ postgres: background writer   
 13769 ?        Ss     0:00  \_ postgres: walwriter   
 13770 ?        Ss     0:00  \_ postgres: autovacuum launcher   
 13771 ?        Ss     0:00  \_ postgres: stats collector   
 13772 ?        Ss     0:00  \_ postgres: logical replication launcher   

相關目錄路徑:

[root@slave01 yum.repos.d]#

[root@slave01 yum.repos.d]# pwd
/var/lib/pgsql/11
[root@slave01 yum.repos.d]# ls
backups  data  initdb.log

RPM包安裝方式簡單,PG軟件目錄及涉及模塊無法調整。

2、源碼包安裝

下載地址:https://www.postgresql.org/ftp/source/

官方文檔:https://www.postgresql.org/docs/11/index.html

可以根據自身情況定製模塊及軟件目錄,軟件名稱:postgresql-11.5.tar.bz2

操作系統安裝選擇最小化安裝,安裝默認模塊的PG需要安裝以下軟件:

 readline-devel zlib zlib-devel

解壓軟件

[root@master soft]# bzip2 -d postgresql-11.5.tar.bz2

[root@master soft]# tar -xvf postgresql-11.5.tar 

解壓完成後,執行源碼編譯三板斧./configure  && make && make install | install-world,不過建議使用gmake world和gmake install-world進行編譯安裝。

添加world選項可以安裝文檔及man幫助和其他附加模塊。

查看檢查相關幫助及涉及模塊選項
 

[root@slave01 postgresql-11.5]# ./configure --help |more
`configure' configures PostgreSQL 11.5 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages

......

 

檢查系統特性及安裝預檢查

[root@master postgresql-11.5]# ./configure 
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432

......

config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port

檢查完成後會生成config.log和config.status文件,日誌中包括檢查及後續編譯涉及的詳見目錄及模塊

Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.r
edhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --e
nable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-styl
e=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj 
--with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gc
c-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-
64 --build=x86_64-redhat-linux

開4個並行進行編譯

[root@master postgresql-11.5]# gmake world  -j 4
gmake -C ./src/backend generated-headers
gmake[1]: 進入目錄“/soft/postgresql-11.5/src/backend”
gmake -C catalog distprep generated-header-symlinks
gmake -C utils distprep generated-header-symlinks

......

開始安裝

[root@master postgresql-11.5]# gmake install-world
gmake -C ./src/backend generated-headers
gmake[1]: 進入目錄“/soft/postgresql-11.5/src/backend”
gmake -C catalog distprep generated-header-symlinks
gmake[2]: 進入目錄“/soft/postgresql-11.5/src/backend/catalog”
gmake[2]: 對“distprep”無需做任何事。
gmake[2]: 對“generated-header-symlinks”無需做任何事。
gmake[2]: 離開目錄“/soft/postgresql-11.5/src/backend/catalog”

......

gmake[2]: 進入目錄“/soft/postgresql-11.5/contrib/vacuumlo”
/usr/bin/mkdir -p '/usr/local/pgsql/bin'
/usr/bin/install -c  vacuumlo '/usr/local/pgsql/bin'
gmake[2]: 離開目錄“/soft/postgresql-11.5/contrib/vacuumlo”
gmake[1]: 離開目錄“/soft/postgresql-11.5/contrib”
PostgreSQL, contrib, and documentation installation complete.

提示安裝成功,可以選擇繼續後續操作。

PG軟件安裝完成後,後續操作不能不能使用root或具有操作系統管理權限的帳號,新建用戶及初始化目錄

[root@master ~]# mkdir -p /postgres/data5435
[root@master ~]# useradd postgres
[root@master ~]# id postgres
uid=1001(postgres) gid=1001(postgres) 組=1001(postgres)

配置用戶環境變量:

export PS1="[\u@\h \W]\$"
export PGPORT=5435
export PGDATA=/postgres/data5435
export LANG=en_US.utf8
export PGHOME=/usr/local/pgsql
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH
export MANPATH=$PGHOME/share/man:$MANPATH
#export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
[postgres@master ~]$ more .bash_profile

創建database cluster,該cluster並非是集羣,稱爲PG實例集合

[postgres@master ~]$initdb --help
initdb initializes a PostgreSQL database cluster.

Usage:
  initdb [OPTION]... [DATADIR]

Options:
  -A, --auth=METHOD         default authentication method for local connections

......

[postgres@master ~]$initdb 
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 "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /postgres/data5435 ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... PRC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

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 /postgres/data5435 -l logfile start

啓動PG數據庫通過pg_ctl:

pg_ctl -D /postgres/data5435 -l logfile start

pg_ctl相關參數及命令:

[postgres@master data5435]$pg_ctl --help
pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.

Usage:
  pg_ctl init[db] [-D DATADIR] [-s] [-o OPTIONS]
  pg_ctl start    [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]
                  [-o OPTIONS] [-p PATH] [-c]
  pg_ctl stop     [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
  pg_ctl restart  [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
                  [-o OPTIONS] [-c]
  pg_ctl reload   [-D DATADIR] [-s]
  pg_ctl status   [-D DATADIR]
  pg_ctl promote  [-D DATADIR] [-W] [-t SECS] [-s]
  pg_ctl kill     SIGNALNAME PID

Common options:
  -D, --pgdata=DATADIR   location of the database storage area
  -s, --silent           only print errors, no informational messages
  -t, --timeout=SECS     seconds to wait when using -w option
  -V, --version          output version information, then exit
  -w, --wait             wait until operation completes (default)
  -W, --no-wait          do not wait until operation completes
  -?, --help             show this help, then exit
If the -D option is omitted, the environment variable PGDATA is used.

Options for start or restart:
  -c, --core-files       allow postgres to produce core files
  -l, --log=FILENAME     write (or append) server log to FILENAME
  -o, --options=OPTIONS  command line options to pass to postgres
                         (PostgreSQL server executable) or initdb
  -p PATH-TO-POSTGRES    normally not necessary

Options for stop or restart:
  -m, --mode=MODE        MODE can be "smart", "fast", or "immediate"

Shutdown modes are:
  smart       quit after all clients have disconnected
  fast        quit directly, with proper shutdown (default)
  immediate   quit without complete shutdown; will lead to recovery on restart

Allowed signal names for kill:
  ABRT HUP INT KILL QUIT TERM USR1 USR2
查看進程情況:

[postgres@master data5435]$ps axf | grep postgres
 7992 pts/0    S      0:00      |   \_ su - postgres
 8089 pts/0    S+     0:00      |           \_ grep --color=auto postgres
 8069 pts/0    S      0:00 /usr/local/pgsql/bin/postgres -D /postgres/data5435
 8071 ?        Ss     0:00  \_ postgres: checkpointer   
 8072 ?        Ss     0:00  \_ postgres: background writer   
 8073 ?        Ss     0:00  \_ postgres: walwriter   
 8074 ?        Ss     0:00  \_ postgres: autovacuum launcher   
 8075 ?        Ss     0:00  \_ postgres: stats collector   
 8076 ?        Ss     0:00  \_ postgres: logical replication launcher 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章