Centos6.8下Postgresql9.6.6安裝與配置
標(biāo)簽:postgresql
1. 環(huán)境介紹
- 系統(tǒng)平臺:CentOS release 6.8 (Final)
- Postgresql:postgresql-9.6.6
- 強制訪問控制系統(tǒng):SELINUX=disabled
- 防火墻:Iptables關(guān)閉
2. 安裝步驟
2.1 安裝依賴包
# 安裝基礎(chǔ)依賴包,用于編譯安裝的基礎(chǔ)環(huán)境
yum -y install gcc*
yum -y install readline-devel
yum -y install zlib-devel
2.2 源碼包獲取
# 使用wget工具拉取軟件源碼包
wget http://ftp.postgresql.org/pub/source/v9.6.6/postgresql-9.6.6.tar.gz
2.3 解壓源碼包
# tar zxf 包名稱 進行解壓
tar zxf postgresql-9.6.6.tar.gz
2.4 添加用戶管理pg
# 創(chuàng)建用戶postgres、設(shè)置密碼 postgres
adduser postgres
passwd postgres
# 根據(jù)提示輸入用戶密碼
2.5 編譯安裝
# cd 到解壓的安裝包目錄進行編譯安裝包
cd postgresql-9.6.6
# 指定包的安裝路徑
./configure --prefix=/home/postgres/pgsql
# 編譯安裝包即可
gmake
gmake install
2.6 設(shè)置環(huán)境變量
# 設(shè)置系統(tǒng)環(huán)境變量
vim /etc/profile
vi /etc/profile
# 添加一行軟件的安裝路徑信息,信息如下
PATH=$PATH:$HOME/bin:/home/postgres/pgsql/bin
# 保存退出,使用source命令是文件立即生效
source /etc/profile
2.7 創(chuàng)建數(shù)據(jù)庫目錄
# 切換到postgre用戶,初始化數(shù)據(jù)庫
su - postgres
# 指定數(shù)據(jù)庫數(shù)據(jù)文件目錄進行初始化設(shè)置
/home/postgres/pgsql/bin/initdb -D /home/postgres/pgsql/data
# 退出postgres用戶,切換到root用戶
exit
2.8 初始化數(shù)據(jù)庫
# 切換到postgre用戶,初始化數(shù)據(jù)庫
su - postgres
# 指定數(shù)據(jù)庫數(shù)據(jù)文件目錄進行初始化設(shè)置
/home/postgres/pgsql/bin/initdb -D /home/postgres/pgsql/data
# 退出postgres用戶,切換到root用戶
exit
2.9 編譯啟動命令
# 從postgres解壓后的文件夾里拷貝linux到/etc/init.d/下
# 注意:/etc/init.d/目錄是相關(guān)服務(wù)的啟動目錄存放文件信息,啟動配置文件
# 拷貝linux到/etc/init.d/目錄下并重命名postgresql
cp /root/postgresql-9.6.6/contrib/start-scripts/linux /etc/init.d/postgresql
# 編輯腳本postgresql信息
vim /etc/init.d/postgresql
vi /etc/init.d/postgresql
# 編輯內(nèi)容主要是修改下面兩行即可。
# pg的安裝目錄
prefix=/home/postgres/pgsql
# pg的數(shù)據(jù)存放目錄
PGDATA="/home/postgres/pgsql/data"
# 保存退出,為postgresql添加可執(zhí)行權(quán)限
chmod +x /etc/init.d/postgresql
2.10 啟動數(shù)據(jù)庫
# 使用postgresql腳本呢啟動postgres數(shù)據(jù)庫
/etc/init.d/postgresql start
# 重啟服務(wù)
/etc/init.d/postgresql restart
2.11 配置服務(wù)器自動啟動
# 注意:postgresql啟動后就可以利用servicepostgresql start、restart或者stop來控制它了。
# 初始化數(shù)據(jù)庫:/etc/init.d/postgresql initdb
# 啟動數(shù)據(jù)庫:/etc/init.d/postgresql start
# 停止數(shù)據(jù)庫:/etc/init.d/postgresql stop
1.把postgresql加入自啟動列表
cd /etc/init.d
chkconfig --add postgresql
1.查看一下自啟動列表:chkconfig --list。
2.11 測試數(shù)據(jù)庫
# [root@MidApp ~]#su - postgres
[postgres@MidApp ~]$ psql
psql (9.6.6)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.utf-8 | en_US.utf-8 |
template0 | postgres | UTF8 | en_US.utf-8 | en_US.utf-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf-8 | en_US.utf-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# \q
2. PostgreSQL 允許遠程訪問設(shè)置方法
2.1 配置對數(shù)據(jù)庫的訪問權(quán)限
# 安裝PostgreSQL數(shù)據(jù)庫之后,默認(rèn)是只接受本地訪問連接。如果想在其他主機上訪問PostgreSQL數(shù)據(jù)庫服務(wù)器,就需要進行相應(yīng)的配置。
# 配置遠 程連接PostgreSQL數(shù)據(jù)庫的步驟很簡單,只需要修改data目錄下的pg_hba.conf和postgresql.conf。
1. pg_hba.conf:配置對數(shù)據(jù)庫的訪問權(quán)限,
2. postgresql.conf:配置PostgreSQL數(shù)據(jù)庫服務(wù)器的相應(yīng)的參數(shù)。
# 配置的步驟:
# 1.找到配置文件所在路徑
find / -name pg_hba.conf
cd /home/postgres/pgsql/data/pg_hba.conf
# 2.編輯pg_hba.conf文件
vi /home/postgres/pgsql/data/pg_hba.conf
# 3.修改pg_hba.conf文件,配置用戶的訪問權(quán)限,在末尾追加如下內(nèi)容即可,然后保存退出。
host all all 0.0.0.0/0 md5
# 注意:這個文件最后有一個列表,它決定了分派了每一個用戶的權(quán)限,以及認(rèn)證方式。格式是“Type Database User Address Method”,要注意的是method最好寫md5
2.2 配置PostgreSQL數(shù)據(jù)庫服務(wù)器的相應(yīng)的參數(shù)
# 修改postgresql.conf文件,將數(shù)據(jù)庫服務(wù)器的監(jiān)聽模式修改為監(jiān)聽所有主機發(fā)出的連接請求。
# 操作步驟:
# 1.找到配置文件所在路徑
find / -name postgresql.conf
cd /home/postgres/pgsql/data/postgresql.conf
# 2.編輯postgresql.conf文件
vi /home/postgres/pgsql/data/postgresql.conf
# 3. 修改內(nèi)容如下:
將listen_addresses前的#去掉,并將listen_addresses ='localhost'改成listen_addresses = '*'
將listen_port前的#去掉,可以修改數(shù)據(jù)庫的端口。
# 保存退出即可
# 查看post相關(guān)進程信息
ps -ef|grep post
# 重啟服務(wù)以使設(shè)置生效
/etc/init.d/postgresql restart
2.3 遠程鏈接不上問題匯總
# 防火墻開啟,需求關(guān)閉(iptables 的基本操作):
# 查看當(dāng)前防火墻狀態(tài)
$ service iptables status
# 查看規(guī)則
$ service iptables
# 關(guān)閉停止服務(wù)
$ service iptables stop
# 開啟服務(wù)
$ service iptables start
# 重啟服務(wù)
$ service iptables restart
3. 登錄數(shù)據(jù)庫相關(guān)配置
3.1 修改數(shù)據(jù)庫密碼
# PostgreSQL 數(shù)據(jù)庫默認(rèn)會創(chuàng)建一個postgres的數(shù)據(jù)庫用戶作為數(shù)據(jù)庫的管理員,默認(rèn)密碼為空,我們需要修改為指定的密碼,這里設(shè)定為’postgres’.
# 在控制臺輸入以下命令:
$ su - postgres
$ psql
# 修改postgres用戶密碼用于遠程登錄
$ ALTER USER postgres WITH PASSWORD 'postgres';
# 查詢數(shù)據(jù)庫
$ select * from pg_shadow ;
# 創(chuàng)建數(shù)據(jù)庫
$ create database demo;
# 鏈接demo數(shù)據(jù)庫
$ \c demo;
$ david=# 寫SQL語句
3.2 Postgresql控制臺命令
# Postgresql的數(shù)據(jù)庫操作基本命令總結(jié):
$ \q:命令退出控制臺
$ \password:為XXX用戶設(shè)置一個密碼。
$ \h:查看SQL命令的解釋,比如\h select。
$ \?:查看psql命令列表。
$ \l:列出所有數(shù)據(jù)庫。
$ \c [database_name]:連接其他數(shù)據(jù)庫。
$ \d:列出當(dāng)前數(shù)據(jù)庫的所有表格。
$ \d [table_name]:列出某一張表格的結(jié)構(gòu)。
$ \du:列出所有用戶。
$ \e:打開文本編輯器。
$ \conninfo:列出當(dāng)前數(shù)據(jù)庫和連接的信息。
4. 參考博文
# Centos6.6下Postgresql9.6.6安裝與配置
1. http://blog.51cto.com/qingmiao/2046357
# 遠程鏈接配置
2. https://blog.csdn.net/yuxuan_08/article/details/51428688