CentOS7安装PostgreSQL

1、下载源代码并解压

wget https://ftp.postgresql.org/pub/source/v9.4.15/postgresql-9.4.15.tar.gz
tar -xvzf postgresql-10.0.tar.gz
yum install readline-devel  
yum install zlib-devel
./configure --with-lib=/usr/lib  --with-includes=/usr/include 
sudo make
sudo make install

2、创建用户组和用户

groupadd postgres    #新增用户组
useradd -g postgres postgres    #新增用户
passwd postgres    #为用户设置密码

3、创建数据目录

mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data    #设置data文件加属性
chmod 700 /usr/local/pgsql/data    #设置data文件夹权限

4、数据库操作

   su postgres
#初始化数据库
   /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data     
#启动\停止\重启数据库 
   /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
   或
   /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start\stop\restart
#设置日志输出位置
   /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &  
#创建测试数据库
   /usr/local/pgsql/bin/createdb test   
#启动测试数据库 
   /usr/local/pgsql/bin/psql test    

5、修改postgresql.conf

进入/usr/local/pgsql/data目录、修改postgresql.conf文件
listen_addresses = '*'
port = 5432

6、修改pg_hba.conf

进入/usr/local/pgsql/data目录、修改pg_hba.conf文件
# "local" is for Unix domain socket connections only
local   all             all                                  trust
# IPv4 local connections:
host    all             all             0.0.0.0/0            trust

7、远程连接

1. 查看防火墙是否关闭:firewall-cmd --state
2. 启动服务:systemctl start firewalld.service
3. 关闭服务:systemctl stop firewalld.service
4. 重启服务:systemctl restart firewalld.service
5. 显示服务的状态:systemctl status firewalld.service
6. 在开机时启用服务:systemctl enable firewalld.service
7. 在开机时禁用服务:systemctl disable firewalld.service
8. 查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?
9. 查看已启动的服务列表:systemctl list-unit-files|grep enabled
10. 添加开发端口:firewall-cmd --zone=public --add-port=5432/tcp --permanent
11. 重新加载防火墙:firewall-cmd --reload

8、链接测试

猜你喜欢

转载自my.oschina.net/OkSerIous/blog/1620999