獲取源碼
安裝PostgreSQL的方法有很多,我們可以使用centos下的包管理器安裝,也可以從官網(wǎng)下載二進制壓縮包安裝,但是很多時候包管理器的倉庫和官網(wǎng)的二進制壓縮包提供的版本過少,不能完全滿足我們的需求。這時候我們可以選擇從官網(wǎng)下載源碼,自己編譯安裝。
從官網(wǎng)下載PostgreSQL源代碼 https://www.postgresql.org/download/,選擇source選項,下載對應(yīng)版本的源碼壓縮包即可,根據(jù)自己的喜愛,可以選擇下載tar.gz格式或者tar.bz2格式的壓縮包。
前期準(zhǔn)備
在編譯之前,我們需要先新增一個操作pg的用戶postgres,以后所有關(guān)于數(shù)據(jù)庫的操作都會使用這個用戶。
#添加新的用戶組
groupadd develop
useradd -d /home/postgres -m -g develop postgres
#為用戶增加密碼
passwd postgres
編譯
安裝過程第一步,我們要配置一些安裝信息,通過解壓后文件夾里的configure文件實現(xiàn)的。如果我們想采用默認的安裝,只需要執(zhí)行這個腳本即可,如果我們想自定義安裝過程,那么我們在執(zhí)行腳本的時候需要加上一下參數(shù),部分參數(shù)如下:
-
--prefix=PREFIX_DIR: 加上這個參數(shù),pg會把所有的編譯后的文件放到這個文件夾中。默認情況下,系統(tǒng)會把這個值設(shè)置成usr/local/pgsql。所以我們要是想把pg安裝到其他文件夾下,只需要在執(zhí)行腳本的時候加上這個參數(shù)即可。(一般來說,自定義安裝只需要用到這個選項) -
--exec-prefix=EXEC-PREFIX:可以通過該選項,將相關(guān)的文件安裝到EXEC-PREFIX文件夾下,而不使用--prefix設(shè)置的地方。這樣做可以比較方便地在不同主機之間共享體系相關(guān)的文件。 -
--bindir=DIRECTORY:可執(zhí)行程序的目錄,如果不指定,默認是EXEC-PREFIX/bin,如果使用默認的--exec-prefix選項,則默認是PREFIX_DIR/bin。 -
--sysconfdir=DIRECTORY:各種配置文件的目錄,默認是PREFIX_DIR/etc。
例
./configure --prefix=/home/postgres/pgsql
上面例子,會把編譯后的文件全部放到/home/postgres/pgsql目錄下,包括可執(zhí)行程序等文件也會放到這個目錄下。
執(zhí)行問configure腳本后,我們就可以執(zhí)行 make命令執(zhí)行源碼。在執(zhí)行源碼之前我們需要檢查是否有c編譯器,如果沒有,我們可以執(zhí)行下面命令進行安裝。
yum -y install gcc
有時候一些系統(tǒng)還缺少readline和zlib庫,這時候我們還要執(zhí)行下面命令安裝。
yum -y install readline-devel
yum install zlib-devel
安裝缺失的包之后就可以編譯源碼,安裝。依次執(zhí)行以下命令:
#這里選擇將pg安裝到這個目錄下面,指定了--prefix之后,pg安裝后文件會全部放到這里
./configure --prefix=/home/postgres/pgsql
make
make install
用戶環(huán)境變量
為之前新建的用戶添加環(huán)境變量,這樣該用戶就能夠直接執(zhí)行命令去操作pg,而不是使用絕對路徑去操作命令。打開新建用戶的.bash_profile文件并在文件后面加入以下內(nèi)容:
#表示pg安裝的目錄,和--prefix的目錄一致
PGHOME=/home/postgres/pgsql
export PGHOME
#pg數(shù)據(jù)目錄,在初始化數(shù)據(jù)庫時如果沒有指定目錄,則選擇環(huán)境變量中的目錄
PGDATA=/home/postgres/pgsql/data
export PGDATA
PATH=$PATH:$HOME/bin:$PGHOME/bin
export PATH
初始化數(shù)據(jù)庫
由于配置了環(huán)境變量,所以此處我們直接執(zhí)行initdb即可完成pg初始化。我們先鍵入initdb --help看一下命令相關(guān)的參數(shù)信息:
tomcat@hw-hadoop1-> initdb --help
initdb initializes a PostgreSQL database cluster.
Usage:
initdb [OPTION]... [DATADIR]
Options:
-A, --auth=METHOD default authentication method for local connections
--auth-host=METHOD default authentication method for local TCP/IP connections
--auth-local=METHOD default authentication method for local-socket connections
[-D, --pgdata=]DATADIR location for this database cluster
-E, --encoding=ENCODING set default encoding for new databases
--locale=LOCALE set default locale for new databases
--lc-collate=, --lc-ctype=, --lc-messages=LOCALE
--lc-monetary=, --lc-numeric=, --lc-time=LOCALE
set default locale in the respective category for
new databases (default taken from environment)
--no-locale equivalent to --locale=C
--pwfile=FILE read password for the new superuser from file
-T, --text-search-config=CFG
default text search configuration
-U, --username=NAME database superuser name
-W, --pwprompt prompt for a password for the new superuser
-X, --xlogdir=XLOGDIR location for the transaction log directory
Less commonly used options:
-d, --debug generate lots of debugging output
-k, --data-checksums use data page checksums
-L DIRECTORY where to find the input files
-n, --noclean do not clean up after errors
-N, --nosync do not wait for changes to be written safely to disk
-s, --show show internal settings
-S, --sync-only only sync data directory
Other options:
-V, --version output version information, then exit
-?, --help show this help, then exit
通過這些參數(shù)后面的介紹,我們可以在數(shù)據(jù)庫初始化的時候指定數(shù)據(jù)目錄、編碼和日志等信息。
例
#初始化時指定了數(shù)據(jù)庫的編碼和數(shù)據(jù)目錄
initdb -E utf8 -D /home/postgres/pgsql/data
啟動和連接
在初始化數(shù)據(jù)庫之后我們要考慮啟動數(shù)據(jù)庫,pg則是使用pg_ctl命令來控制數(shù)據(jù)庫的啟動和關(guān)閉。
postgres@hw-hadoop1-> 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 [-w] [-t SECS] [-D DATADIR] [-s] [-l FILENAME] [-o "OPTIONS"]
pg_ctl stop [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
pg_ctl restart [-w] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
[-o "OPTIONS"]
pg_ctl reload [-D DATADIR] [-s]
pg_ctl status [-D DATADIR]
pg_ctl promote [-D DATADIR] [-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 until operation completes
-W do not wait until operation completes
-?, --help show this help, then exit
(The default is to wait for shutdown, but not for start or restart.)
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 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"
從幫助文檔我們可以看到,使用pg_ctl命令可以幫我們啟動、重啟和關(guān)閉數(shù)據(jù)庫。同時我們可以在啟動的時候使用-D來指定數(shù)據(jù)庫數(shù)據(jù)的目錄,在下面的介紹我們可以看到,如果我們沒有指定這個選項,那么就會使用環(huán)境變量中配置的目錄,也就是我們之前在環(huán)境變量中配置PGDATA;我們也可以使用-l選項來指定日志的存儲地點,以便我們方便查看數(shù)據(jù)庫的日志信息。
啟動數(shù)據(jù)庫之后我們就可以使用psql命令連入數(shù)據(jù)庫中,進入數(shù)據(jù)庫我們首先要做的就是設(shè)置postgres用戶的密碼,鍵入\password,輸入密碼即可。
做完這些基本上就能夠使用了,但是這樣只能夠允許我們在本機使用數(shù)據(jù)庫,其他的網(wǎng)絡(luò)是不能夠連接到我們數(shù)據(jù)庫的,所以我們要設(shè)置數(shù)據(jù)庫允許遠程訪問。
-
修改postgresql.conf文件
找到安裝目錄下的postgresql.conf文件,并且在文件中找到listen_addresses選項,將其改成如下所示內(nèi)容:
listen_addresses = '*'
-
修改pg_hba.conf文件
在與postgresql.conf文件相同的目錄下找到該配置文件,打開該文件,編輯或添加下面一行。(也可以改成你允許連接的ip)
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 0.0.0.0/0 md5
重啟數(shù)據(jù)庫即可。
配置主從
主/從數(shù)據(jù)庫復(fù)制是將數(shù)據(jù)從主數(shù)據(jù)庫復(fù)制到另外一個或者多個數(shù)據(jù)庫的過程。主要好處是能夠?qū)?shù)據(jù)分發(fā)到不同的數(shù)據(jù)庫中,當(dāng)主數(shù)據(jù)庫出現(xiàn)故障時,可以請求從數(shù)據(jù)庫中的數(shù)據(jù),保證不會出現(xiàn)請求的中斷。同樣,我們也可以使用從數(shù)據(jù)庫來分擔(dān)主數(shù)據(jù)庫的壓力,加快數(shù)據(jù)的處理速度。
postgres在9.0之后引入了主從流復(fù)制機制,從數(shù)據(jù)庫通過tcp流從主數(shù)據(jù)庫中同步相應(yīng)的數(shù)據(jù)。
配置主從前我們要準(zhǔn)備兩臺裝了相同版本的 postgresql的服務(wù)器,假設(shè)我們有兩臺裝了pg的服務(wù)器,ip分別為172.30.20.1,172.30.20.2,我們規(guī)定172.30.20.1上面裝的數(shù)據(jù)庫為主庫,172.30.20.2上面的數(shù)據(jù)庫為從庫。
配置主數(shù)據(jù)庫
1、首先在數(shù)據(jù)庫安裝目錄下創(chuàng)建一個存放歸檔的文件夾
#數(shù)據(jù)庫安裝目錄為/home/postgres/pgsql
mkdir /home/postgres/pgsql/archive
2、我們需要一個數(shù)據(jù)庫用戶進行主從同步。在主數(shù)據(jù)創(chuàng)建用戶,賦予登錄和復(fù)制的權(quán)限
#密碼根據(jù)個人自己定義
create role replica login replication encrypted password '123456'
3、允許新建的用戶進行同步
在data目錄下找到配置文件pg_hba.conf,在配置文件中加入如下兩行
host all all 172.30.20.2/32 trust #允許從服務(wù)器連接到主服務(wù)器
host replication replica 172.30.20.2/32 md5 #允許從服務(wù)器使用replica用戶來復(fù)制
4、修改postgresql.conf配置文件
找到配置文件postgresql.conf,修改配置文件,如下所示的選項進行修改
listen_addresses = '*' # 監(jiān)聽所有IP
archive_mode = on # 允許歸檔
archive_command = 'cp %p /home/postgres/pgsql/archive/%f' # 用該命令來歸檔logfile segment
wal_level = hot_standby
max_wal_senders = 32 # 這個設(shè)置了可以最多有幾個流復(fù)制連接,差不多有幾個從,就設(shè)置幾個wal_keep_segments = 256 # 設(shè)置流復(fù)制保留的最多的xlog數(shù)目
wal_sender_timeout = 60s # 設(shè)置流復(fù)制主機發(fā)送數(shù)據(jù)的超時時間
max_connections = 100 # 這個設(shè)置要注意下,從庫的max_connections必須要大于主庫的
配置完上面選項之后就可以重啟數(shù)據(jù)庫,接著配置從數(shù)據(jù)庫。
從數(shù)據(jù)庫配置
1、為了保證基礎(chǔ)數(shù)據(jù)的一致,我們先從主數(shù)據(jù)庫將起data目錄下的數(shù)據(jù)復(fù)制到從數(shù)據(jù)庫的data目錄下
#先將data目錄下的數(shù)據(jù)都清空
rm -rf /home/postgres/pgsql/data/*
# 從主數(shù)據(jù)庫拷貝數(shù)據(jù)到從數(shù)據(jù)庫(基礎(chǔ)備份)
pg_basebackup -h 172.30.20.1 -U replica -D /home/postgresql/pgsql/data -X stream -P
#新建歸檔存儲的文件夾
mkdir /home/postgres/pgsql/archive
2、配置recovery.conf
將安裝目錄下share目錄下的recovery.conf.sample文件復(fù)制到data目錄下,并改名為recovery.conf,修改文件。
standby_mode = on # 說明該節(jié)點是從服務(wù)器
primary_conninfo = 'host=172.30.20.1 port=5432 user=replica password=123456' # 主數(shù)據(jù)庫的信息以及連接的用戶
recovery_target_timeline = 'latest'
3、配置postgresql.conf
找到配置文件postgresql.conf,修改配置文件,如下所示的選項進行修改
wal_level = hot_standby
max_connections = 1000 # 一般查多于寫的應(yīng)用從庫的最大連接數(shù)要比較大
hot_standby = on # 說明這臺機器不僅僅是用于數(shù)據(jù)歸檔,也用于數(shù)據(jù)查詢
max_standby_streaming_delay = 30s # 數(shù)據(jù)流備份的最大延遲時間
wal_receiver_status_interval = 10s # 多久向主報告一次從的狀態(tài),當(dāng)然從每次數(shù)據(jù)復(fù)制都會向主報告狀態(tài),這里只是設(shè)置最長的間隔時間
hot_standby_feedback = on # 如果有錯誤的數(shù)據(jù)復(fù)制,是否向主進行反饋
配置完就可以重啟從服務(wù)器。
驗證主從是否配置成功
1、在主數(shù)據(jù)庫上執(zhí)行一下sql,即可查看相應(yīng)的從數(shù)據(jù)庫的信息
select client_addr,sync_state from pg_stat_replication;
2、通過查看兩個服務(wù)器的進程來判斷,分別在兩個服務(wù)器上執(zhí)行命令ps -ef | grep postgres,在主服務(wù)器上我們可以看到一個walsender的進程;在從服務(wù)器上我們可以看到一個walreceiver的進程,這樣就證明主從配置完成。