微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CentOS 7 安装 PostgreSQL 教程

CentOS的源中自带有Postgresql,可以通过 yum list | grep postgresql 查看系统自带的版本。

1、安装 yum 源(地址从 http://yum.postgresql.org/repopackages.php 获取

yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

2、安装Postgresql

这里最核心的是要安装postgresql96-server和postgresql96-contrib,其中”contrib”包里包含了一些常用的组件和方法

yum install postgresql96-server postgresql96-contrib

安装后,可执行文件在 /usr/pgsql-9.6/bin/, 数据和配置文件在 /var/lib/pgsql/9.6/data/

3、初始化数据库

/usr/pgsql-9.6/bin/postgresql96-setup initdb

如果指定数据目录执行下列命令:

[root@localhost ~]# sudo -i -u postgres /usr/pgsql-9.6/bin/initdb -D /data/pg/pgdata

4、认情况下Postgresql不支持密码登录,如需支持需要修改配置文件

vi /var/lib/pgsql/9.6/data/pg_hba.conf

将未注释行中的ident 替换为 md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD
 
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost,by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

如需开启远程访问,可编辑/var/lib/pgsql/9.6/data/postgresql.conf 文件

将 #listen_addresses = 'localhost' 修改为 listen_addresses='' (此处‘’也可以改为你想开放的服务器IP)

另外对于特定的IP还可以设置开启信任远程连接,修改/var/lib/pgsql/9.6/data/pg_hba.conf,按下面的格式进行添加设置。

# IPv4 local connections:
 host    all            all      127.0.0.1/32      trust
 host    all            all      8.8.8.8/32(需要连接的服务器IP)  trust

修改完配置以后不要忘了重启服务。

5、管理服务

systemctl start postgresql-9.6 #启动服务
systemctl restart postgresql-9.6 #重启服务
systemctl stop postgresql-9.6 #停止服务
systemctl enable postgresql-9.6 #自动启动

6、登录Postgresql

Postgresql 安装完成后,会建立一个‘postgres’用户,用于执行Postgresql数据库中也会建立一个’postgres’用户,如果我们要使用Postgresql就必须先登录此帐号。

sudo -i -u postgres

执行后提示符会变为 ‘-bash-4.2$’,再运行

同构执行进入 psql 进入postgresql命令行环境。

[root[@localhost~]# sudo -i -u postgres
-bash-4.2$ psql
psql (9.6.1)
Type "help" for help.
 
postgres=#

接着可以执行 ALTER USER postgres WITH PASSWORD '123456' 来设置postgres用户密码,可通过 \q 退出数据库

7、打开防火墙

CentOS 防火墙中内置了Postgresql服务,配置文件位置在/usr/lib/firewalld/services/postgresql.xml,只需以服务方式将Postgresql服务开放即可。

firewall-cmd --add-service=postgresql --permanent  开放postgresql服务
firewall-cmd --reload  重载防火墙

原文地址:https://www.jb51.cc/centos/375057.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。