PostGreSQL(1)-源碼安裝

簡述

目前大多數Linux 發行版都包含了 PostGreSql的安裝源,經過yumapt-get能夠快速的安裝好數據庫。
在一些狀況下,咱們須要經過源碼方式進行安裝。 例如發行版的軟件版本不知足咱們的需求,或者生產環境沒法對接外網下載軟件等等。
下面將從頭至尾介紹手動源碼安裝PostGres的全過程,供你們參考。linux

1、格式化磁盤

通常咱們會爲數據庫掛載一個獨立的磁盤用於數據存放,能夠參考下面的操做完成磁盤格式化。web

不須要掛載磁盤的可跳過這部分sql

A.查看磁盤信息數據庫

kwe1000570040:~ # fdisk -l

Disk /dev/vda: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000aa73e

   Device Boot Start End Blocks Id System
/dev/vda1 * 2048 75505663 37751808 83 Linux
/dev/vda2 75505664 83886079 4190208 82 Linux swap / Solaris

Disk /dev/vdb: 322.1 GB, 322122547200 bytes

這裏找到待使用的磁盤爲 /dev/vdbide

B.使用parted設置分區工具

//設置分區格式(gpt)
parted -s /dev/vdb mklabel gpt


//執行分區(1GB預留) --第一個分區用於存放工做軟件
parted -a optimal -s /dev/vdb mkpart primary 1GB 100GB

//執行分區 --第二個分區用於存放數據
parted -a optimal -s /dev/vdb mkpart extend 100GB 100%

關於 Parted工具的使用可參考這裏post

C. 格式化並掛載分區性能

推薦使用XFS文件系統格式,在掛載分區時指定noatime,nodiratime選項可提高一些性能。編碼

//XFS數據分區格式化
mkfs.xfs -f /dev/vdb1 
mkfs.xfs -f /dev/vdb2 

//建立數據目錄
mkdir -p /opt/local
mkdir -p /data

chmod -R 755 /opt/local
chmod -R 755 /data

//寫入FSTAB
echo "/dev/vdb1 /opt/local xfs noatime,nodiratime 0 0">> "/etc/fstab"
echo "/dev/vdb2 /data xfs noatime,nodiratime 0 0">> "/etc/fstab"

//掛載目錄
mount -a

2、源碼安裝 PostGreSql

1. 安裝 readline-devel

需事先安裝 readline,不然編譯過程會報錯

wget https://ftp.gnu.org/gnu/readline/readline-6.0.tar.gz
tar -xzvf readline-6.0.tar.gz
cd readline
./configure
make && make install

2. 安裝 PostGresql

執行下面的命令,將會完成源碼下載、編碼以及安裝

wget https://ftp.postgresql.org/pub/source/v10.6/postgresql-10.6.tar.gz
tar -xzvf postgresql-10.6.tar.gz
cd postgresql-10.6

./configure --prefix=/opt/local/postgres
make && make install

--prefix表示安裝的目標路徑,在安裝完成後能夠進入該目錄找到對應的執行程序。

這裏使用的是postgresql-10.6的版本,能夠從這裏下載到不一樣的版本。

3. 設置環境變量

編輯 /etc/profile 文件,在末尾加入:

export PG_HOME=/opt/local/postgres
export PATH=$PG_HOME/bin:$PATH

再次執行 source /etc/profile即完成環境變量設置。
接下來,須要設置動態連接庫的加載路徑(LD_LIBRARY_PATH),不然會報找不到libpq.so.5的錯誤,以下:

./psql: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory

設置連接庫路徑

sudo /sbin/ldconfig /opt/local/pgsql/lib

或經過編輯/etc/profile,加入:

LD_LIBRARY_PATH=/usr/local/pgsql/lib
export LD_LIBRARY_PATH

3、 初始化

1. 設置運行用戶

groupadd postgres
useradd -G postgres postgres
passwd postgres
...

2. 初始化數據庫

//執行初始化
su postgres -c 'pg_ctl -D /data/pgsql/data initdb'
//啓動數據庫
su postgres -c 'pg_ctl start -D /data/pgsql/data -l serverlog'

su postgres -c '...'表示以postgres 的權限來執行程序。
pg_ctl 是postgres 提供的控制程序,可用於初始化、啓動、中止數據庫進程。

執行完上述的命令,能夠發如今 /data/pgsql/data目錄中生成了一系列的數據庫文件,以下:

-rw------- 1 postgres users 3 Mar 5 17:18 PG_VERSION
drwx------ 6 postgres users 50 Mar 5 18:22 base
drwx------ 2 postgres users 4.0K Mar 5 17:50 global
drwx------ 2 postgres users 6 Mar 5 17:18 pg_commit_ts
drwx------ 2 postgres users 6 Mar 5 17:18 pg_dynshmem
-rw------- 1 postgres users 4.5K Mar 5 17:18 pg_hba.conf
-rw------- 1 postgres users 1.6K Mar 5 17:18 pg_ident.conf
drwx------ 4 postgres users 65 Mar 5 18:28 pg_logical
drwx------ 4 postgres users 34 Mar 5 17:18 pg_multixact
drwx------ 2 postgres users 17 Mar 5 17:49 pg_notify
drwx------ 2 postgres users 6 Mar 5 17:18 pg_replslot
drwx------ 2 postgres users 6 Mar 5 17:18 pg_serial
drwx------ 2 postgres users 6 Mar 5 17:18 pg_snapshots
drwx------ 2 postgres users 6 Mar 5 17:49 pg_stat
drwx------ 2 postgres users 60 Mar 7 16:06 pg_stat_tmp
drwx------ 2 postgres users 17 Mar 5 17:18 pg_subtrans
drwx------ 2 postgres users 6 Mar 5 17:18 pg_tblspc
drwx------ 2 postgres users 6 Mar 5 17:18 pg_twophase
drwx------ 3 postgres users 58 Mar 5 17:18 pg_wal
drwx------ 2 postgres users 17 Mar 5 17:18 pg_xact
-rw------- 1 postgres users 88 Mar 5 17:18 postgresql.auto.conf
-rw------- 1 postgres users 23K Mar 5 17:18 postgresql.conf
-rw------- 1 postgres users 57 Mar 5 17:49 postmaster.opts
-rw------- 1 postgres users 83 Mar 5 17:49 postmaster.pid
-rw-r--r-- 1 postgres users 1.2K Mar 5 18:22 serverlog

其中,須要瞭解的幾個文件:

文件 描述
postmaster.pid 首行記錄了進程PID
serverlog 數據庫日誌
postgresql.conf 主配置文件(可作定製
pg_hba.conf 鑑權相關文件
PG_VERSION 當前主版本號

3. 設置遠程訪問

編輯 pg_hba.conf文件,在末尾添加一行:

host all all 0.0.0.0/0 md5

爲了讓 postgres用戶能夠遠程訪問,能夠經過 psql 設置密碼:

alter user postgres with password 'postgres';

開啓遠程訪問
默認狀況下 postgresql 僅僅監聽本機的端口,須要編輯 /webdata/pgsql/data/postgresql.conf 文件開啓遠程IP的訪問

listen_addresses = '*'

4. 經常使用命令

若是須要定製端口,能夠執行腳本:

postgres -p 5430 -D /data/pgsql/data >serverlog 2>&1 &

檢查進程是否存活:

netstat -nlp |grep `head -1 /data/pgsql/data/postmaster.pid`

中止數據庫進程

kill -INT `head -1 /data/pgsql/data/postmaster.pid`

4、 自動運行

數據庫通常須要設置爲隨系統啓動運行,省去每次重啓要手動拉起進程的麻煩。

1. 配置到服務

找到源碼目錄中 contrib/start-scripts/linux腳本文件,拷貝爲 /etc/init.d/postgres

修改內容以下:

# 程序所在目錄
prefix=/opt/local/postgres

# 數據目錄
PGDATA="/data/pgsql/data"

# 運行用戶
PGUSER=postgres

# 日誌文件
PGLOG="$PGDATA/serverlog"

設置執行權限

chmod +x /etc/init.d/postgres

此後,執行如下命令能夠方便的啓停服務

//手動啓動服務
service postgres start

//查看服務狀態
service postgres status

//手動中止服務
service postgres stop

2. 設置自啓動

執行如下命令:

//設置開機啓動
chkconfig --add postgres

若是須要對數據庫作一些參數修改,能夠編輯 $DATA_DIR/postgres.conf 這個文件

5、 小試牛刀

按照前面的步驟作好了數據庫的安裝及配置以後,輸入如下命令:

psql -U postgres -p 5432

> select version();

回車,能夠看到如下輸出:

version
---------------------------------------------------------------------------------------------------------------------
 PostgreSQL 10.6 on x86_64-pc-linux-gnu, compiled by gcc (SUSE Linux) 4.3.4 [gcc-4_3-branch revision 152973], 64-bit
(1 row)

至此,咱們就已經完成了整個源碼的安裝過程!

相關文章
相關標籤/搜索