linux 下安裝 postgresql-9.3.1.tar.gz
一、 環境
centos6.0 minimal
postgresql-9.3.1.tar.gz
二、准備工作
- 1將虛擬機網卡使用NAT模式,方便上網
- 2關閉防火牆,可以查看02.centos常用操作.md
三、先安裝 make, gcc ,gcc-c++,readline-devel ,zlib-devel 。如果已安裝,可以忽略
這些都是依賴。
[root@linhp local]# yum -y install gcc
[root@linhp local]# yum -y install gcc-c++
[root@linhp local]# yum -y install readline-devel
[root@linhp local]# yum -y install zlib-devel
[root@linhp postgresql-9.3.1]# yum -y install make
四、開始安裝
4.1 解壓 tar -zvxf postgresql-9.3.1.tar.gz
我的postgresql-9.3.1.tar.gz 安裝包放在 /usr/local 下
[root@linhp local]# pwd
/usr/local
[root@linhp local]# tar -zvxf postgresql-9.3.1.tar.gz
...解壓過程
[root@linhp local]# cd postgresql-9.3.1
[root@linhp postgresql-9.3.1]#
4.2 創建linux用戶及密碼
我的密碼設置為123456
[root@linhp postgresql-9.3.1]# adduser postgres
[root@linhp postgresql-9.3.1]# passwd postgres
Changing password for user postgres.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@linhp postgresql-9.3.1]#
4.3 開始安裝
[root@linhp postgresql-9.3.1]# pwd
/usr/local/postgresql-9.3.1
// 配置
[root@linhp postgresql-9.3.1]# ./configure --prefix=/usr/local/postgresql
// 編譯
[root@linhp postgresql-9.3.1]# make
//安裝
[root@linhp postgresql-9.3.1]# make install
4.4 配置環境變量
在環境變量 /etc/profile 最后加入
PATH=$PATH:/usr/local/postgresql/bin
[root@linhp postgresql]# cd /usr/local/postgresql
[root@linhp postgresql]# ls
bin include lib share
[root@linhp postgresql]# vi /etc/profile
在文件最后加入:PATH=$PATH:/usr/local/postgresql/bin
然后刷新環境變量,即時生效
[root@linhp postgresql]# source /etc/profile
輸出環境變量,查看是否設置成功
[root@linhp postgresql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/java/jdk1.8.0_91/bin:/opt/java/jdk1.8.0_91/jre/bin:/root/bin:/opt/java/jdk1.8.0_91/bin:/opt/java/jdk1.8.0_91/jre/bin:/usr/local/postgresql/bin
4.5 初始化數據庫
[root@linhp postgresql]# pwd
/usr/local/postgresql
[root@linhp postgresql]# mkdir data
[root@linhp postgresql]# ls
bin data include lib share
[root@linhp postgresql]# chown postgres:postgres /usr/local/postgresql/data/
[root@linhp postgresql]# su postgres
[postgres@linhp postgresql]$ /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/```
4.6 復制並修改配置文件(修改存放數據目錄)
切換到root
復制安裝目錄下的linux文件到/etc/init.d/中,並將linux名稱重命名為postgresql
[root@linhp postgresql-9.3.1]# cp /usr/local/postgresql-9.3.1/contrib/start-scripts/linux /etc/init.d/postgresql
編輯復制出來的文件,修改下圖中的位置即可
[root@linhp postgresql-9.3.1]# vi /etc/init.d/postgresql

[root@linhp postgresql-9.3.1]# chmod +x /etc/init.d/postgresql
4.7 啟動數據庫,和設置開機自啟
[root@linhp postgresql-9.3.1]# /etc/init.d/postgresql start
Starting PostgreSQL: ok
[root@linhp postgresql-9.3.1]# chkconfig postgresql on
[root@linhp postgresql-9.3.1]#
4.8 創建數據庫操作的歷史文件
創建文件,並授予postgres權限
[root@linhp postgresql-9.3.1]# touch /usr/local/postgresql/.pgsql_history
[root@linhp postgresql-9.3.1]# chown postgres:postgres /usr/local/postgresql/.pgsql_history
[root@linhp postgresql-9.3.1]#
4.9 測試數據庫是否創建成功,並且連接數據庫
使用 \q 即可退出數據庫連接
[root@linhp postgresql-9.3.1]# su postgres
[postgres@linhp postgresql-9.3.1]$ createdb test
[postgres@linhp postgresql-9.3.1]$ psql test
psql (9.3.1)
Type "help" for help.
test=# \q
[postgres@linhp postgresql-9.3.1]$
到此,數據庫已經安裝成功。
五、修改數據庫外網訪問
Linux 修改PostgreSQL外部訪問白名單
先關閉防火牆,或者開放5432端口
主要修改兩個配置文件
[root@linhp postgresql-9.3.1]# find / -name pg_hba.conf
[root@linhp postgresql-9.3.1]# find / -name postgresql.conf
5.1 先關閉數據庫服務
關閉數據庫服務
[postgres@linhp postgresql-9.3.1]$ su
Password:
[root@linhp postgresql-9.3.1]# /etc/init.d/postgresql stop
Stopping PostgreSQL: ok
[root@linhp postgresql-9.3.1]#
5.2 修改pg_hba.conf
將默認的
host all all 127.0.0.1/32 trust
修改為
host all all 192.168.1.1/32 trust #IP全匹配
或
host all all 192.168.1.1/24 trust #IP匹配前三段
或
host all all 0.0.0.0/0 trust #全部允許
5.3修改postgresql.conf
listen_addresses 默認是注釋掉的,打開即可
修改默認的
listen_addresses = 'localhost'
為
listen_addresses = '*'
如果需要修改端口,可以修改 listen_addresses下一行
5.4 重啟postgresql服務
/etc/init.d/postgresql start
5.5 測試本地連接 ( 這里我修改了端口為5444)
[postgres@linhp postgresql-9.3.1]$ psql -h 127.0.0.1 -d postgres -U postgres -p 5444psql
psql (9.3.1)
Type "help" for help.
postgres=#
