PostgreSQL 安裝

簡(jiǎn)述

以前用習(xí)慣了MySQL,來(lái)到新公司開(kāi)始接觸PostgreSQL,這個(gè)名字讀起來(lái)可真拗口呀。
鑒于MySQL和PostgreSQL都是目前比較流行的開(kāi)源免費(fèi)數(shù)據(jù)庫(kù),也被各個(gè)互聯(lián)網(wǎng)公司廣泛使用,甚至一些傳統(tǒng)型的公司也開(kāi)始放棄Oracle,轉(zhuǎn)投MySQL和PostgreSQL的懷抱。

至于兩者之間的比較和對(duì)比,由于初次接觸PostgreSQL,暫時(shí)還不便評(píng)論,先從最初始的安裝走起。

環(huán)境準(zhǔn)備

Host List

IP Address Hosts Disk Comment
192.168.0.22 db 1TB Database Host

OS

我們采用CentOS作為宿主機(jī)操作系統(tǒng),版本請(qǐng)升級(jí)為最新穩(wěn)定版7.6。(CentOS7即可,建議升級(jí)到最新穩(wěn)定版7.6)
升級(jí)方式,請(qǐng)參考本人的另一篇文章:http://m.itdecent.cn/p/3e3bc1f51332

并將內(nèi)核升級(jí)到最新穩(wěn)定版本4.20.

[root@localhost ~]# uname -sr
Linux 4.20.0-1.el7.elrepo.x86_64
[root@localhost ~]# 
[root@localhost ~]# 

安裝步驟

看了PG的官網(wǎng),本來(lái)準(zhǔn)備安裝最新的PG12,結(jié)果官網(wǎng)說(shuō)PG12現(xiàn)在剛剛做了崩潰測(cè)試,甚至還沒(méi)有Alpha和Beta測(cè)試,建議繼續(xù)使用PG10,那我們就老老實(shí)實(shí)的繼續(xù)PG10吧。

下載安裝

首先安裝yaml包

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

安裝PG Server。

yum install postgresql10-contrib postgresql10-server -y

此時(shí),進(jìn)入到/usr目錄應(yīng)該可以看到PG10的目錄。

[root@localhost usr]# ls -al
total 176
drwxr-xr-x.  14 root     root      4096 Dec 28 13:50 .
dr-xr-xr-x.  17 root     root      4096 Jan  7 15:09 ..
dr-xr-xr-x.   2 root     root     28672 Dec 28 13:50 bin
drwxr-xr-x.   2 root     root         6 Apr 11  2018 etc
drwxr-xr-x.   2 root     root         6 Apr 11  2018 games
drwxr-xr-x.   3 root     root        22 Apr 11  2018 include
dr-xr-xr-x.  47 root     root      8192 Dec 28 10:45 lib
dr-xr-xr-x.  53 root     root     36864 Dec 28 11:29 lib64
drwxr-xr-x.  25 root     root      4096 Dec 28 10:45 libexec
drwxr-xr-x.  12 root     root      4096 Apr 11  2018 local
drwxr-xr-x    6 root     root    48 Dec 28 13:50 pgsql-10
dr-xr-xr-x.   2 root     root     16384 Dec 28 11:29 sbin
drwxr-xr-x. 118 root     root      4096 Dec 28 11:29 share
drwxr-xr-x.   4 root     root        32 Apr 11  2018 src
lrwxrwxrwx    1 root     root        10 Dec 28 10:43 tmp -> ../var/tmp
[root@localhost usr]# 

至此已安裝完畢,接下來(lái)我們對(duì)數(shù)據(jù)庫(kù)進(jìn)行初始化。

初始化數(shù)據(jù)庫(kù)

準(zhǔn)備數(shù)據(jù)庫(kù)存儲(chǔ)目錄

由于PG會(huì)默認(rèn)采用/data作為數(shù)據(jù)庫(kù)數(shù)據(jù)文件存儲(chǔ)路徑,我們運(yùn)維把磁盤空間全部mount到/home目錄,暫時(shí)不能重新mount,如果是自己的環(huán)境,可以重新規(guī)劃磁盤。所以我們需要將PG的數(shù)據(jù)盤符路徑調(diào)整到/home目錄下。重申:這是研發(fā)測(cè)試環(huán)境,如果生產(chǎn)環(huán)境,還請(qǐng)采用標(biāo)準(zhǔn)的磁盤規(guī)劃。

[root@localhost ~]# mkdir /home/postgresql
[root@localhost ~]# mkdir /home/postgresql/data

增加PG相關(guān)用戶并賦權(quán)

我們需要?jiǎng)?chuàng)建PG的專屬用戶,并將PG相關(guān)的目錄全部轉(zhuǎn)歸該用戶下。
yum安裝postgresql,默認(rèn)會(huì)建一個(gè)名為”postgres”的系統(tǒng)賬號(hào),用于執(zhí)行PostgreSQL;
可以查詢下,如果沒(méi)有創(chuàng)建,則手工創(chuàng)建下。

[root@localhost ~]# useradd postgres
useradd: user 'postgres' already exists
[root@localhost ~]# 

調(diào)整相關(guān)目錄的權(quán)限。

[root@localhost ~]#  chown -R postgres:postgres /home/postgresql
[root@localhost ~]#  chmod 750 -R /home/postgresql
[root@localhost ~]# chown -R postgres:postgres /usr/pgsql-10

修改Root用戶的profile,增加PG相關(guān)的環(huán)境變量。

[root@localhost ~]# vi .bash_profile 
export LD_LIBRARY_PATH=/usr/pgsql-10/bin
export PGDATA=/home/postgresql/data

修改postgres用戶的環(huán)境變量。

[root@localhost ~]# su postgres
bash-4.2$ cd ~
bash-4.2$ ls -al
total 16
drwx------   3 postgres postgres   75 Dec 28 14:28 .
drwxr-xr-x. 41 root     root     4096 Dec 28 13:50 ..
drwx------   4 postgres postgres   31 Dec 28 13:50 10
-rw-------   1 postgres postgres  500 Dec 28 14:28 .bash_history
-rwx------   1 postgres postgres  265 Dec 28 14:02 .bash_profile
-rw-------   1 postgres postgres   23 Dec 28 14:28 .psql_history
bash-4.2$ vi .bash_profile
[ -f /etc/profile ] && source /etc/profile
PGDATA=/home/postgresql/data
export PGDATA
......

執(zhí)行初始化。

bash-4.2$ cd /usr/pgsql-10/bin
bash-4.2$ ./postgresql-10-setup initdb

設(shè)置自啟動(dòng)

退出postgres用戶,用root用戶執(zhí)行如下命令。

[root@localhost ~]# systemctl enable postgresql-10

修改PG啟動(dòng)的參數(shù),修改PGDATA路徑。

[root@localhost ~]# vi /usr/lib/systemd/system/postgresql-10.service
......
# Location of database directory
Environment=PGDATA=/home/postgresql/data/
......
[root@localhost ~]# systemctl restart postgresql-10
[root@localhost ~]# systemctl status postgresql-10
● postgresql-10.service - PostgreSQL 10 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-10.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-01-07 15:08:58 CST; 20h ago
     Docs: https://www.postgresql.org/docs/10/static/
  Process: 15371 ExecStartPre=/usr/pgsql-10/bin/postgresql-10-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 15410 (postmaster)
    Tasks: 8
   Memory: 15.8M

查看PG的狀態(tài)為active running,就說(shuō)明PG啟動(dòng)完成。

修改PG的運(yùn)行參數(shù)

經(jīng)過(guò)上面的步驟,我們的PG Server已可以啟動(dòng),只是還只能本機(jī)訪問(wèn)。接下來(lái)我們對(duì)PG進(jìn)行參數(shù)調(diào)整。
找到PG Data的目錄,然后修改其中的postgresql.conf.

......
listen_addresses = '*'
......

由于本例為研發(fā)測(cè)試環(huán)境,不做生成環(huán)境用,參數(shù)調(diào)整就不做詳細(xì)展開(kāi)了。

重啟PG。

[root@localhost ~]# systemctl restart postgresql-10

創(chuàng)建Schema和用戶

注意替換自己的數(shù)據(jù)庫(kù)用戶密碼。

bash-4.2$ psql
psql (10.6)
Type "help" for help.

postgres=# CREATE USER harbor WITH PASSWORD '******';
CREATE ROLE
postgres=# 
postgres=# CREATE DATABASE harbor OWNER harbor;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE harbor TO harbor;
GRANT
postgres=# 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 安裝PostgreSQL數(shù)據(jù)庫(kù)(Linux篇) 編譯環(huán)境 Linux: CentOS 5.5 # yum inst...
    XuDongTian閱讀 1,317評(píng)論 0 6
  • pg_ctl 名稱 pg_ctl -- 啟動(dòng)、停止、重啟 PostgreSQL語(yǔ)法 pg_ctl start [-...
    老肖閱讀 1,020評(píng)論 0 0
  • 客戶端安裝sudo apt-get install postgresql-client 服務(wù)器安裝sudo apt...
    代碼界的小學(xué)生閱讀 2,194評(píng)論 0 1
  • 我是爸爸媽媽的臭丫頭,整天懶懶散散,十分氣人,大過(guò)年,人人都高興,我卻手一松把媽媽新買的杯子弄?dú)埩?,我總是玩忘?..
    枃氼侖麼灬閱讀 401評(píng)論 0 1
  • 感恩一直被美景縈繞著,震撼著,感動(dòng)著,渲染著,浸醉在美的海洋中,內(nèi)心無(wú)比的柔軟平靜而愉悅。感恩這美的感覺(jué)真的是有效...
    開(kāi)荒者cx閱讀 266評(píng)論 0 2

友情鏈接更多精彩內(nèi)容