簡述
以前用習慣了MySQL干旧,來到新公司開始接觸PostgreSQL渠欺,這個名字讀起來可真拗口呀。
鑒于MySQL和PostgreSQL都是目前比較流行的開源免費數據庫椎眯,也被各個互聯網公司廣泛使用挠将,甚至一些傳統(tǒng)型的公司也開始放棄Oracle,轉投MySQL和PostgreSQL的懷抱编整。
至于兩者之間的比較和對比舔稀,由于初次接觸PostgreSQL,暫時還不便評論掌测,先從最初始的安裝走起内贮。
環(huán)境準備
Host List
IP Address | Hosts | Disk | Comment |
---|---|---|---|
192.168.0.22 | db | 1TB | Database Host |
OS
我們采用CentOS作為宿主機操作系統(tǒng),版本請升級為最新穩(wěn)定版7.6汞斧。(CentOS7即可夜郁,建議升級到最新穩(wěn)定版7.6)
升級方式,請參考本人的另一篇文章:http://www.reibang.com/p/3e3bc1f51332
并將內核升級到最新穩(wěn)定版本4.20.
[root@localhost ~]# uname -sr
Linux 4.20.0-1.el7.elrepo.x86_64
[root@localhost ~]#
[root@localhost ~]#
安裝步驟
看了PG的官網断箫,本來準備安裝最新的PG12拂酣,結果官網說PG12現在剛剛做了崩潰測試,甚至還沒有Alpha和Beta測試仲义,建議繼續(xù)使用PG10婶熬,那我們就老老實實的繼續(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
此時赵颅,進入到/usr目錄應該可以看到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]#
至此已安裝完畢暂刘,接下來我們對數據庫進行初始化饺谬。
初始化數據庫
準備數據庫存儲目錄
由于PG會默認采用/data作為數據庫數據文件存儲路徑,我們運維把磁盤空間全部mount到/home目錄,暫時不能重新mount募寨,如果是自己的環(huán)境族展,可以重新規(guī)劃磁盤。所以我們需要將PG的數據盤符路徑調整到/home目錄下拔鹰。重申:這是研發(fā)測試環(huán)境仪缸,如果生產環(huán)境,還請采用標準的磁盤規(guī)劃列肢。
[root@localhost ~]# mkdir /home/postgresql
[root@localhost ~]# mkdir /home/postgresql/data
增加PG相關用戶并賦權
我們需要創(chuàng)建PG的專屬用戶恰画,并將PG相關的目錄全部轉歸該用戶下。
yum安裝postgresql瓷马,默認會建一個名為”postgres”的系統(tǒng)賬號拴还,用于執(zhí)行PostgreSQL;
可以查詢下欧聘,如果沒有創(chuàng)建片林,則手工創(chuàng)建下。
[root@localhost ~]# useradd postgres
useradd: user 'postgres' already exists
[root@localhost ~]#
調整相關目錄的權限树瞭。
[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相關的環(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
設置自啟動
退出postgres用戶,用root用戶執(zhí)行如下命令凉敲。
[root@localhost ~]# systemctl enable postgresql-10
修改PG啟動的參數衣盾,修改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爷抓,就說明PG啟動完成势决。
修改PG的運行參數
經過上面的步驟,我們的PG Server已可以啟動蓝撇,只是還只能本機訪問果复。接下來我們對PG進行參數調整。
找到PG Data的目錄渤昌,然后修改其中的postgresql.conf.
......
listen_addresses = '*'
......
由于本例為研發(fā)測試環(huán)境虽抄,不做生成環(huán)境用,參數調整就不做詳細展開了独柑。
重啟PG迈窟。
[root@localhost ~]# systemctl restart postgresql-10
創(chuàng)建Schema和用戶
注意替換自己的數據庫用戶密碼。
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=#