PostgreSQL數據庫安裝方法總結

1 引言

PostgreSQL數據庫如今變得愈來愈流行,在DB-Engines網站(https://db-engines.com/en/ranking ),排名第四,本人所在的公司也是極力推廣PG數據庫,開源社區中,PG的活躍度也是很是高,本文簡單介紹一下pg數據庫的幾種安裝方法。linux

2 安裝

2.1 部署規劃

本文總結了PostgreSQL數據庫的三種安裝方法,都是基於CentOS7.6版原本進行,能夠經過/etc/redhat-release來查看具體的版本。sql

[root@pgDatabase ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

對於數據庫的目錄規劃,並無嚴格的規定,可是生產環境中,仍是建議將數據目錄放在掛載的盤中,防止因爲系統的問題致使數據庫沒法啓動,數據丟失。數據庫

目錄 存放位置
home目錄 /home/postgres
安裝目錄 /usr/local/
數據目錄 /data/pgdata

安裝文件介紹和下載連接,默認版本爲pg9.6。bootstrap

安裝方式 安裝包名稱 下載地址
tar.gz文件解壓直接安裝 postgresql-9.6.13-4-linux-x64-binaries.tar.gz https://www.enterprisedb.com/download-postgresql-binaries
源碼編譯安裝 postgresql-9.6.10.tar.gz http://ftp.postgresql.org/pub/source/
Rpm包安裝 postgresql-server、postgresql-contrib、postgresql-libs、postgresql https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/

2.2 解壓安裝

2.2.1 建立postgres用戶

useradd postgres
passwd postgres

登錄一下,讓系統建立家目錄bash

su - postgres

2.2.2 建立目錄

mkdir -p /data/pgdata
chown -R postgres:postgres /data/pgdata/
chmod -R 775 /data/pgdata/

2.2.3 解壓安裝

tar -xzvf postgresql-9.6.13-4-linux-x64-binaries.tar.gz -C /data/
chown -R postgres /data/pgsql

解壓後的文件夾名字是pgsql服務器

建立軟鏈接ide

cd /usr/local
ln -s /data/pgsql pgsql

配置環境變量post

cd /home/postgres
vi .bash_profile
# 在path後面添加數據庫的bin目錄方便啓動數據庫
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin

2.2.4 初始化數據庫

su - postgres
initdb -D /data/pgdata/

修改配置文件網站

cd /data/pgdata/
vi postgresql.conf
# 將該條配置反註釋,將localhost改爲*
listen_addresses = '*'

修改訪問控制文件this

vi pg_hba.conf
# IPv4 local connections:
host    all             all             0.0.0.0/0            md5

說明:

# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
# "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
# "password" sends passwords in clear text; "md5" is preferred since
# it sends encrypted passwords.

關閉防火牆

systemctl stop firewalld

2.2.5 啓動數據庫

[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
server starting

進入數據庫修改postgres密碼

psql
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE

這樣就算所有完成了,遠程也能夠鏈接數據庫了。

2.3 源碼安裝

2.3.1 下載源碼包

若是服務器能夠聯網,可使用wget命令來下載。或者從網站上下載而後傳到服務器也能夠。

wget http://ftp.postgresql.org/pub/source/v9.6.13/postgresql-9.6.13.tar.gz

2.3.2 安裝依賴包

安裝前必需要保證gcc編譯器已經安裝好。可使用rpm -qa gcc檢查。

yum install gcc make openssl zlib-devel -y

2.3.3 解壓安裝包

tar -xzvf postgresql-9.6.13.tar.gz -C /usr/local

2.3.4 編譯並安裝

cd /usr/local/postgresql-9.6.13/
./configure --prefix=/usr/local/pgsql

問題

...
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
...

按照提示後面加上便可--without-readline

./configure --prefix=/usr/local/pgsql --without-readline

安裝,該過程比較長,通常在5~10分鐘左右,安裝完後,會在/usr/local目錄下看到pgsql文件夾,也就是安裝完後的數據庫的位置。

make && make install

2.3.5 建立postgres用戶

useradd postgres
passwd postgres

登錄一下,讓系統建立家目錄

su - postgres

配置環境變量

cd /home/postgres
vi .bash_profile
# 在path後面添加數據庫的bin目錄方便啓動數據庫
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin

2.3.6 建立目錄

mkdir -p /data/pgdata
chown -R postgres:postgres /data/pgdata/
chmod -R 700 /data/pgdata/

2.3.7 初始化數據庫

su - postgres
initdb -D /data/pgdata/

輸出如下內容則代表成功。

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.UTF-8".
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 /data/pgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
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 /data/pgdata/ -l logfile start

修改配置文件

cd /data/pgdata/
vi postgresql.conf
# 將該條配置反註釋,將localhost改爲*
listen_addresses = '*'

修改訪問控制文件

vi pg_hba.conf
# IPv4 local connections:
host    all             all             0.0.0.0/0            md5

說明:

# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
# "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
# "password" sends passwords in clear text; "md5" is preferred since
# it sends encrypted passwords.

關閉防火牆

systemctl stop firewalld
systemctl disable firewalld

2.3.8 啓動數據庫

[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
server starting

進入數據庫修改postgres密碼

psql
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE

這樣就算所有完成了,遠程也能夠鏈接數據庫了。

2.4 RPM安裝

2.4.1 下載rpm包

使用rpm安裝須要下載上面所列出的4個rpm包,下載的時候須要注意版本的一致。

wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm
wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm
wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm
wget https://yum.postgresql.org/9.6/redhat/rhel-7.6-x86_64/postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm

2.4.2 安裝依賴包

yum install gcc make openssl zlib-devel libxslt -y

2.4.3 安裝rpm包

注意,須要依次安裝,包之間有依賴關係。

[root@mydb ~]# rpm -ivh postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm 
warning: postgresql96-libs-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql96-libs-9.6.13-1PGDG.rh################################# [100%]
[root@mydb ~]# rpm -ivh postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm 
warning: postgresql96-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql96-9.6.13-1PGDG.rhel7  ################################# [100%]
[root@mydb ~]# rpm -ivh postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm 
warning: postgresql96-server-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql96-server-9.6.13-1PGDG.################################# [100%]
[root@mydb ~]# rpm -ivh postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm 
warning: postgresql96-contrib-9.6.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:postgresql96-contrib-9.6.13-1PGDG################################# [100%]

2.4.4 建立目錄

mkdir -p /data/pgdata
chown -R postgres:postgres /data/pgdata/
chmod -R 775 /data/pgdata/

配置環境變量

cd /home/postgres
vi .bash_profile
# 在path後面添加數據庫的bin目錄方便啓動數據庫
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/pgsql-9.6/bin/

2.4.5 初始化數據庫

su - postgres
cd /usr/pgsql-9.6/bin/
initdb -D /data/pgdata/

2.4.6 配置啓動數據庫

修改配置文件

cd /data/pgdata/
vi postgresql.conf
# 將該條配置反註釋,將localhost改爲*
listen_addresses = '*'

修改訪問控制文件

vi pg_hba.conf
# IPv4 local connections:
host    all             all             0.0.0.0/0            md5

說明:

# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
# "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
# "password" sends passwords in clear text; "md5" is preferred since
# it sends encrypted passwords.

關閉防火牆

systemctl stop firewalld
systemctl disable firewalld

2.4.7 啓動數據庫

[postgres@pgDatabase ~]$ pg_ctl -D /data/pgdata/ -l logfile start
server starting

進入數據庫修改postgres密碼

psql
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE

這樣就算所有完成了,遠程也能夠鏈接數據庫了。

3 總結

以上三種方法就是最經常使用的安裝數據庫的方式,固然也有更爲簡單的是直接yum install postgresql,可是這種方法只適合臨時進行部署一個,不推薦做爲生產環境,通常生產環境還會根據實際狀況配置高可用的主從方式,採用流複製的方式來提升數據庫可用性。

相關文章
相關標籤/搜索