CentOS7安装配置PostgreSQL数据库服务器

步骤摘要

    yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
    yum install postgresql96-server postgresql96-contrib
    /usr/pgsql-9.6/bin/postgresql96-setup initdb
    systemctl start postgresql-9.6
    systemctl enable postgresql-9.6
    firewall-cmd --add-service=postgresql --permanent
    firewall-cmd --reload
    su - postgres
    psql -U postgres
    ALTER USER postgres with encrypted password 'abc123';
    \q
    exit
    vi /var/lib/pgsql/9.6/data/postgresql.conf
    vi /var/lib/pgsql/9.6/data/pg_hba.conf
    systemctl restart postgresql-9.6.service

步骤详情

安装yum源

打开https://yum.postgresql.org/repopackages.php ,找到自己需要的版本。

本文撰写时,PostgreSQL 10还是测试版,因此在此演示9.6版。
右键复制链接地址。

不放心是否复制成功的话可以粘贴出来看看。

以root模式进入CentOS7,输入“yum install ”后面加上刚刚复制的链接,回车。

输入“y”,回车。

安装PostgreSQL

输入“yum install postgresql96-server postgresql96-contrib”并回车。
(如果使用其他版本的PostgreSQL则需要把其中的两个“96”换成对应的数字)

输入“y”并回车。

安装后,可执行文件在“/usr/pgsql-9.5/bin/”, 数据和配置文件在“/var/lib/pgsql/9.6/data/”。因此输入“/usr/pgsql-9.6/bin/postgresql96-setup initdb”并回车,初始化数据库。

输入“systemctl start postgresql-9.6”并回车,启动服务。
输入“systemctl enable postgresql-9.6”并回车,设为开机自启。
(如果使用其他版本的PostgreSQL则需要把其中的两个“9.6”换成对应的版本)

输入“firewall-cmd –add-service=postgresql –permanent”并回车,开放防火墙。
输入“firewall-cmd –reload”并回车,重启防火墙。

修改默认PostgreSQL用户密码

  • PostgreSQL的默认用户为“postgres”。
  • 输入“su – postgres”并回车,切换至用户。
  • 输入“psql -U postgres”并回车,登录数据库。
  • 输入“ALTER USER postgres with encrypted password ‘abc123’;”(不要漏了“;”)并回车,设置默认用户postgre的密码,此处密码为“abc123”,可自行修改。
  • 输入“\q”并回车, 退出数据库。
  • 输入“exit”并回车,退出用户。

配置远程访问

输入“vi /var/lib/pgsql/9.6/data/postgresql.conf”并回车。
(如果使用其他版本的PostgreSQL则需要把其中的“9.6”换成对应的版本)
光标下翻,找到“listen_addresses”。

按“i”键进入插入编辑模式,如果想对所有IP开放,则将“localhost”改为“*”即可,如果想仅对部分IP开放,多个IP之间用“, ”(逗号+空格)隔开。

改完之后去掉“listen_address”前面的“#”。

编辑完成后,按“Esc”键,输入“:wq”并回车。
输入“vi /var/lib/pgsql/9.6/data/pg_hba.conf”并回车,将光标移至底部。
(如果使用其他版本的PostgreSQL则需要把其中的“9.6”换成对应的版本)

按“i”键进入插入编辑模式,在“IPv4 local connections”下方添加允许连接的IP。
如果想允许所有IPv4地址,则加入一行“host all all 0.0.0.0/0 md5”。IPv6方法类似。

编辑完成后,按“Esc”键,输入“:wq”并回车。
输入“systemctl restart postgresql-9.6.service”并回车,重启服务。
(如果使用其他版本的PostgreSQL则需要把其中的“9.6”换成对应的版本)

标签:PostgreSQLCentos 发布于:2019-11-16 22:03:48