PostgreSQL是一個(gè)功能強(qiáng)大的開源對(duì)象關(guān)系數(shù)據(jù)庫管理系統(tǒng)(ORDBMS),在開源數(shù)據(jù)庫使用上與MySQL各領(lǐng)風(fēng)騷。

PostgreSQL是一個(gè)功能強(qiáng)大的開源對(duì)象關(guān)系數(shù)據(jù)庫管理系統(tǒng)(ORDBMS),在開源數(shù)據(jù)庫使用上與MySQL各領(lǐng)風(fēng)騷。但也有不少人質(zhì)疑postgresql的未來,正所謂,贊揚(yáng)或批判一種數(shù)據(jù)庫都必須先了解它,然后才可有話語權(quán)。為了更多的了解postgresql,我們就先部署一套實(shí)例作為了解它的基礎(chǔ)。
一 、 環(huán)境介紹
操作系統(tǒng): centos
CPU: 4核
內(nèi)存: 16G
postgresql: postgresql-11.4
二、 編譯安裝
1、安裝依賴包
yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
2、下載并解壓
wget https://ftp.postgresql.org/pub/source/v11.4/postgresql-11.4.tar.gz
tar -zxvf postgresql-11.4.tar.gz
3、創(chuàng)建用戶
# 查看用戶是否存在
id postgres
# 添加用戶組及用戶
groupadd postgres
useradd -g postgres postgres
# 再次查看可以查看對(duì)應(yīng)的uid gid
id postgres

4、創(chuàng)建 postgresql數(shù)據(jù)目錄并授權(quán)
選擇對(duì)應(yīng)的磁盤空間較大的盤創(chuàng)建數(shù)據(jù)目錄
mkdir -p /data/postgresql/data
chown -R postgres:postgres data
5、編譯postgresql源碼
cd /data/postgresql-11.4
./configure --prefix=/data/postgresql

PostgreSQL配置腳本選項(xiàng)

6、開始安裝
編譯后結(jié)果如下:

至此,postgresql安裝完成。
7、配置環(huán)境變量
# 切換到postgres用戶
su - postgres
# 編輯postgres用戶環(huán)境變量
vim .bash_profile
# 添加如下內(nèi)容
export PGHOME=/data/postgresql
export PGDATA=/data/postgresql/data
PATH=$PATH:$HOME/bin:$PGHOME/bin
# 使環(huán)境變量生效
source .bash_profile

8、初始化數(shù)據(jù)庫
在postgres用戶下運(yùn)行initdb命令即可初始化數(shù)據(jù)庫。

此時(shí),postgresql數(shù)據(jù)目錄下已經(jīng)生成對(duì)應(yīng)的文件。
cd /data/postgresql/data
ll -h

9、配置文件修改
修改數(shù)據(jù)目錄下的postgresql.conf 及 pg_hba.conf文件。
postgresql.conf 配置PostgreSQL數(shù)據(jù)庫服務(wù)器的相應(yīng)的參數(shù)。 pg_hba.conf 配置對(duì)數(shù)據(jù)庫的訪問權(quán)限。
初期測(cè)試使用時(shí),可以簡(jiǎn)單修改部分配置,其他值使用默認(rèn)值。
(1)修改 postgresql.conf
vim postgresql.conf
修改 listen_addresses 為 * ,代表所有主機(jī)皆可訪問
listen_addresses = '*'

內(nèi)存配置等參數(shù)后續(xù)將介紹其含義及配置建議。
(2)修改 pg_hba.conf
添加如下記錄:

10、配置服務(wù)
如需配置為服務(wù)啟動(dòng)方式,可以按照如下步驟操作。
# 進(jìn)入postgresql源碼目錄
cd /data/postgresql-11.4/contrib/start-scripts
# 此目錄下有各系統(tǒng)的啟動(dòng)目錄,需先將其添加執(zhí)行權(quán)限
chmod +x linux
# 將啟動(dòng)服務(wù)拷貝至啟動(dòng)服務(wù)下
cp linux /etc/init.d/postgresql

因啟動(dòng)服務(wù)命令里配置上了默認(rèn)安裝路徑目錄及數(shù)據(jù)目錄,如與默認(rèn)路徑不一致,需手動(dòng)調(diào)整。

vim /etc/init.d/postgresql修改 prefix及PGDATA

11、啟動(dòng)服務(wù)
通過服務(wù)啟動(dòng)postgresql:
/etc/init.d/postgresql start

通過服務(wù)關(guān)閉postgresql:
/etc/init.d/postgresql stop
通過pg_ctl 啟動(dòng):
# 將postgresql安裝路徑bin目錄下的命令賦權(quán)給postgres用戶
cd /data/postgresql/bin
chown -R postgres:postgres .
# 切換至postgres用戶啟動(dòng)服務(wù)
su - postgres
# 啟動(dòng)服務(wù)
pg_ctl -D /data/postgresql/data/ -l logfile start

至此,便可以通過客戶端連接數(shù)據(jù)庫進(jìn)行操作了。
三、簡(jiǎn)單操作
1、創(chuàng)建數(shù)據(jù)庫
2、連接數(shù)據(jù)庫
# 使用psql連接gjc數(shù)據(jù)庫
psql gjc
3、創(chuàng)建表、索引、并插入數(shù)據(jù)
gjc=# create table test1(id int not null primary key,name varchar(20),age int );
CREATE TABLE
gjc=# create index idx_test1_name on test1(name);
CREATE INDEX
gjc=# insert into test1 values(1,'gjc',28);
INSERT 0 1
gjc=# select * from test1
id | name | age
----+------+-----
1 | gjc | 28
(1 row)
其他的操作,大家可以參考官方文檔或中文社區(qū)進(jìn)行學(xué)習(xí)實(shí)踐。