一、 环境
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端口
一般linux中默认安装了firewall,若 firewalld 未安装,则先进行安装,安装请另查
启动firewall:
systemctl start firewalld # 启动
systemctl status firewalld #查看firewalld当前状态
systemctl enable firewalld # 开机启动
systemctl stop firewalld # 关闭
systemctl disable firewalld # 取消开机启动
查看开放端口命令:
firewall-cmd --list-ports
添加开放端口:
firewall-cmd --zone=public --add-port=5000/tcp --permanent #对外网全部开放
firewall-cmd --zone=public --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.0.1/24 port port=22 protocol=tcp accept' #添加端口开放
firewall-cmd --permanent --remove-rich-rule 'rule family=ipv4 source address=192.168.0.1/2 port port=5000 protocol=tcp accept' #删除端口开放
或使用简单的开放端口命令:
# 允许 tcp 5432 端口
sudo firewall-cmd --permanent --add-port=5432/tcp
reload防火墙,应用规则
sudo systemctl reload firewalld
主要修改两个配置文件
[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服务
重启postgresql命令:sudo /etc/init.d/postgresql restart
启动postgresql命令:sudo /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=#