一、基本信息
官網 https://www.postgresql.org/
下載 https://www.postgresql.org/download/
中文社區 http://www.postgres.cn/index.php/v2/home
易百教程 https://www.yiibai.com/postgresql
二、系統及工具
1、系統版本 Centos7.5 CentOS-7-x86_64-Minimal-1804
2、VMware 版本:VMware Workstation Pro15
3、工具:xshell5
4、PostgreSql: postgresql-11.4.tar.gz
三、安裝部署
1、安裝基本工具
[root@localhost ~]# yum install -y vim lrzsz tree wget gcc gcc-c++ readline-devel
2、創建目錄,並進入指定目錄
[root@localhost ~]# mkdir /opt/postgresql
[root@localhost ~]# cd /opt/postgresql
3、下載
[root@localhost postgresql]# wget https://ftp.postgresql.org/pub/source/v11.4/postgresql-11.4.tar.gz
4、解壓、查看目錄
[root@localhost postgresql]# tar zxvf postgresql-11.4.tar.gz
5、編譯
[root@localhost postgresql-11.4]# ./configure --prefix=/usr/local/postgresql
6、安裝
[root@localhost postgresql-11.4]# make && make install
7、進入安裝後的目錄,查看目錄結構
[root@localhost ~]# cd /usr/local/postgresql/
8、創建目錄 data、log
[root@localhost postgresql]# mkdir /usr/local/postgresql/data
[root@localhost postgresql]# mkdir /usr/local/postgresql/log
9、加入系統環境變量
[root@localhost ~]# vim /etc/profile
在最後寫入如下內容
PGHOME=/usr/local/postgresql
export PGHOME
PGDATA=/usr/local/postgresql/data
export PGDATA
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
export PATH
使配置文件生效
[root@localhost ~]# source /etc/profile
10、增加用戶 postgres 並賦權
[root@localhost ~]# adduser postgres
[root@localhost ~]# chown -R postgres:root /usr/local/postgresql/
11、初始化數據庫
切換用戶 postgres
[root@localhost ~]# su postgres
[postgres@localhost root]$ /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
注意:
不能在 root 用戶下初始數據庫,否則會報錯
[root@localhost ~]# /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
12、編輯配置文件
[postgres@localhost ~]# vim /usr/local/postgresql/data/postgresql.conf
listen_addresses = '*'
port = 5432
[postgres@localhost ~]$ vim /usr/local/postgresql/data/pg_hba.conf
說明:
TYPE:pg的連接方式,local:本地unix套接字,host:tcp/ip連接
DATABASE:指定數據庫
USER:指定數據庫用戶
ADDRESS:ip地址,可以定義某臺主機或某個網段,32代表檢查整個ip地址,相當於固定的ip,24代表只檢查前三位,最後一 位是0~255之間的任何一個
METHOD:認證方式,常用的有ident,md5,password,trust,reject。
md5是常用的密碼認證方式。
password是以明文密碼傳送給數據庫,建議不要在生產環境中使用。
trust是隻要知道數據庫用戶名就能登錄,建議不要在生產環境中使用。
reject是拒絕認證。
13、啓動服務
[postgres@localhost ~]$ pg_ctl start -l /usr/local/postgresql/log/pg_server.log
14、查看版本
[postgres@localhost ~]$ psql -V
psql (PostgreSQL) 11.4
15、登錄數據庫
[postgres@localhost ~]$ psql -U postgres -d postgres
第三方工具測試連接
注意:生產環境,一定要修改密碼驗證方式
至此,Centos7 安裝 PostgreSql 安裝部署完畢!