侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

CentOS 7下安装PostgreSQL 9.6的教程分享

linux  /  管理员 发布于 7年前   227

前言

PostgreSQL是一个强大开源的对象关系类型数据库系统,它能运行于几乎所有主要的操作系统,包括Linux、Unix(AIX、BSD、HP-UX、SGI IRIX、Mac OS、Solaris、Tru64)、Windows OS。对于新的项目我是非常建议使用PostgreSQL。这里就简单的整理下CentOS下的安装过程。

CentOS的源中自带有PostgreSQL,可以通过 yum list | grep postgresql 查看系统自带的版本,我这边看到的是9.2版本,个人还是比较建议安装更新的版本。

安装

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

这里选择的是最新的9.6版本,安装完毕后使用 yum update 更新源信息,然后再使用 yum list | grep postgresql ,是否已经有最新版本的postgresql。

[root@localhost ~]# yum list|grep postgresqlcalligra-kexi-driver-postgresql.x86_64 2.9.10-1.el7     epel  collectd-postgresql.x86_64    5.6.0-1.el7     epel  freeradius-postgresql.x86_64   3.0.4-6.el7     base  libreoffice-postgresql.x86_64   1:4.3.7.2-5.el7_2.1   updates mingw32-postgresql.noarch    9.3.4-2.el7     epel  mingw64-postgresql.noarch    9.3.4-2.el7     epel  opendbx-postgresql.x86_64    1.4.6-6.el7     epel  opensips-postgresql.x86_64    1.10.5-3.el7     epel  owncloud-postgresql.noarch    9.0.4-1.el7     epel  pcp-pmda-postgresql.x86_64    3.10.6-2.el7     base  pdns-backend-postgresql.x86_64   3.4.10-1.el7     epel  perdition-postgresql.x86_64    2.1-5.el7      epel  postgresql.i686       9.2.15-1.el7_2     updates postgresql.x86_64      9.2.15-1.el7_2     updates postgresql-contrib.x86_64    9.2.15-1.el7_2     updates postgresql-devel.i686     9.2.15-1.el7_2     updates postgresql-devel.x86_64     9.2.15-1.el7_2     updates postgresql-docs.x86_64     9.2.15-1.el7_2     updates postgresql-jdbc.noarch     9.4.1212-1.rhel7    pgdg96 postgresql-jdbc-javadoc.noarch   9.4.1212-1.rhel7    pgdg96 postgresql-libs.i686     9.2.15-1.el7_2     updates postgresql-libs.x86_64     9.2.15-1.el7_2     updates postgresql-odbc.x86_64     09.03.0100-2.el7    base  postgresql-pgpool-II.x86_64    3.4.6-1.el7     epel  postgresql-pgpool-II-devel.x86_64  3.4.6-1.el7     epel  postgresql-pgpool-II-extensions.x86_64 3.4.6-1.el7     epel  postgresql-plperl.x86_64    9.2.15-1.el7_2     updates postgresql-plpython.x86_64    9.2.15-1.el7_2     updates postgresql-plruby.x86_64    0.5.3-13.el7     epel  postgresql-plruby-doc.x86_64   0.5.3-13.el7     epel  postgresql-pltcl.x86_64     9.2.15-1.el7_2     updates postgresql-server.x86_64    9.2.15-1.el7_2     updates postgresql-test.x86_64     9.2.15-1.el7_2     updates postgresql-unit96.x86_64    1.0-1.rhel7     pgdg96 postgresql-unit96-debuginfo.x86_64  1.0-1.rhel7     pgdg96 postgresql-upgrade.x86_64    9.2.15-1.el7_2     updates postgresql96.x86_64      9.6.1-1PGDG.rhel7    pgdg96 postgresql96-contrib.x86_64    9.6.1-1PGDG.rhel7    pgdg96 postgresql96-debuginfo.x86_64   9.6.1-1PGDG.rhel7    pgdg96 postgresql96-devel.x86_64    9.6.1-1PGDG.rhel7    pgdg96 postgresql96-docs.x86_64    9.6.1-1PGDG.rhel7    pgdg96 postgresql96-libs.x86_64    9.6.1-1PGDG.rhel7    pgdg96 postgresql96-odbc.x86_64    09.05.0400-1PGDG.rhel7   pgdg96 postgresql96-plperl.x86_64    9.6.1-1PGDG.rhel7    pgdg96 postgresql96-plpython.x86_64   9.6.1-1PGDG.rhel7    pgdg96 postgresql96-pltcl.x86_64    9.6.1-1PGDG.rhel7    pgdg96 postgresql96-python.x86_64    4.2-1PGDG.rhel7    pgdg96 postgresql96-python-debuginfo.x86_64 4.2-1PGDG.rhel7    pgdg96 postgresql96-server.x86_64    9.6.1-1PGDG.rhel7    pgdg96 postgresql96-tcl.x86_64     2.1.1-1.rhel7     pgdg96 postgresql96-tcl-debuginfo.x86_64  2.1.1-1.rhel7     pgdg96 postgresql96-test.x86_64    9.6.1-1PGDG.rhel7    pgdg96 proftpd-postgresql.x86_64    1.3.5b-2.el7     epel  python-testing.postgresql.noarch  1.1.0-2.el7     epel  qt-postgresql.i686      1:4.8.5-12.el7_2    updates qt-postgresql.x86_64     1:4.8.5-12.el7_2    updates qt5-qtbase-postgresql.x86_64   5.6.1-3.el7     epel  soci-postgresql.x86_64     3.2.3-1.el7     epel  soci-postgresql-devel.x86_64   3.2.3-1.el7     epel

2、安装PostgreSQL

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

yum install postgresql96-server postgresql96-contrib

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

3、初始化数据库

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

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 onlylocal 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、管理服务,和其他服务一样还是用systemd进行管理

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$ psqlpsql (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 重载防火墙

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。


  • 上一条:
    Linux下创建Postgresql数据库的方法步骤
    下一条:
    Linux CentOS 7源码编译安装PostgreSQL9.5
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客