如何在 RHEL 8/CentOS 8 上安装 PostgreSQL 服务器如何在 RHEL 8/CentOS 8 上安装 PostgreSQL 服务器如何在 RHEL 8/CentOS 8 上安装 PostgreSQL 服务器如何在 RHEL 8/CentOS 8 上安装 PostgreSQL 服务器
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

如何在 RHEL 8/CentOS 8 上安装 PostgreSQL 服务器

发表 admin at 2025年2月28日
类别
  • 未分类
标签

PostgreSQL 是一个免费开源的对象关系数据库管理系统。本教程的目标是在 RHEL 8/CentOS 8 Linux 服务器上执行 PostgreSQL 服务器的安装和基本配置。

在本教程中您将学习:

  • 如何在 RHEL 8/CentOS 8 上安装 PostgreSQL 数据库服务器

  • 如何启动并启用 PostgreSQL 数据库服务器

  • 如何从本地主机和远程位置访问 PostgreSQL 数据库

  • 如何为默认 postgres 用户设置密码

  • 如何启用 PostgreSQL 监听所有网络

  • 如何通过 MD5 密码身份验证保护 PostgreSQL 远程连接

  • 如何打开PostgreSQL防火墙端口

  • 如何使用psql客户端与PostgreSQL服务器建立远程连接

在 Red Hat Enterprise Linux 8 上初始化和访问 PostgreSQL 数据库

本地 PostgreSQL 安装和数据库访问分步说明

  1. 安装 PostreSQL 服务器。执行以下 dnf 命令来执行 PostreSQL 服务器包安装:

    # dnf install postgresql-server
    
  2. 初始化 PostgreSQL 数据库:

    # postgresql-setup --initdb --unit postgresql
     * Initializing database in '/var/lib/pgsql/data'
     * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
    
  3. 启动 PostgreSQL 并可选择使其在重新引导后启动。

    # systemctl start postgresql
    # systemctl enable postgresql
    

    此时,PostreSQL 服务器应该已启动并正在运行,并且正在侦听本地主机端口 5432。使用 ss 命令来确认情况是否如此:

    $ ss -nlt
    State  Recv-Q  Send-Q     Local Address:Port     Peer Address:Port  
    LISTEN 0       128              0.0.0.0:111           0.0.0.0:*     
    LISTEN 0       32         192.168.122.1:53            0.0.0.0:*     
    LISTEN 0       128              0.0.0.0:22            0.0.0.0:*     
    LISTEN 0       128            127.0.0.1:5432          0.0.0.0:*     
    LISTEN 0       128                 [::]:111              [::]:*     
    LISTEN 0       128                 [::]:22               [::]:*     
    LISTEN 0       128                [::1]:5432             [::]:*
    
  4. 访问 PostreSQL 数据库。当您在 RHEL 8/CentOS 8 系统上安装 PostgreSQL 数据库时,安装程序还将自动创建一个新的默认用户 postgres。

    postgres 用户的默认密码未设置,因此为空。要访问PostgreSQL数据库,首先以root用户执行su命令切换到postres用户。然后,输入 psql 登录数据库。

    注意
    任何以 root 用户身份访问 PostgreSQL 数据库的尝试都将导致出现 psql: FATAL: role "root" does not exit 错误消息。

    例子 :

    # su - postgres
    $ psql
    psql (10.5)
    Type "help" for help.
    
    postgres=#
    

    注意
    要退出 PostreSQL 数据库 shell,请键入 \q 或按 CTRL+d 组合键。

PostgreSQL数据库远程访问和安全连接

  1. 为 postgres 用户设置密码。为了远程访问 PostreSQL 服务器,我们首先为 postres 用户设置密码:

    # su - postgres
    $ psql
    psql (10.5)
    Type "help" for help.
    
    postgres=# \password postgres
    Enter new password: 
    Enter it again: 
    postgres=# exit
    postgres-# \q
    
  2. 启用 PostgreSQL 服务器侦听所有可用网络。编辑主配置文件 /var/lib/pgsql/data/postgresql.conf:

    # nano /var/lib/pgsql/data/postgresql.conf
    

    准备好后,将以下行添加到 CONNECTIONS AND AUTHENTICATION 部分:

    listen_addresses = '*'
    

    警告
    上述配置将使 PostreSQL 能够侦听所有可用网络。建议设置更严格的规则,以便仅允许从选定的网络访问 PostgreSQL。

    使用 ss 命令确认 PostgreSQL 正在监听 0.0.0.0 网络:

    $ ss -nlt
    State  Recv-Q  Send-Q     Local Address:Port     Peer Address:Port  
    LISTEN 0       128              0.0.0.0:111           0.0.0.0:*     
    LISTEN 0       32         192.168.122.1:53            0.0.0.0:*     
    LISTEN 0       128              0.0.0.0:22            0.0.0.0:*     
    LISTEN 0       128            0.0.0.0:5432          0.0.0.0:*     
    LISTEN 0       128                 [::]:111              [::]:*     
    LISTEN 0       128                 [::]:22               [::]:*     
    LISTEN 0       128                [::]:5432             [::]:*
    
  3. 启用 MD5 加密的密码验证:

    # echo "host all all 0.0.0.0/0  md5" >> /var/lib/pgsql/data/pg_hba.conf
    
  4. 应用 PostgreSQL 配置更改:

    # systemctl restart postgresql
    
  5. 为远程 PostgreSQL 传入流量打开防火墙端口 5432:

    # firewall-cmd --zone=public --permanent --add-service=postgresql
    # firewall-cmd --reload
    
  6. 从远程位置连接到 PostgreSQL 数据库服务器。首先在远程主机上安装 psql PostgreSQL 客户端工具:

    RHEL/CENTOS
    # dnf install postgresql
    UBUNTU/DEBIAN
    # apt install postgresql-client
    

    创建到主机的远程连接,例如。 192.168.1.151 作为 postgres 用户和用户密码,如上面第 1 步中所定义:

    $ psql -h 192.168.1.151 -U postgres
    Password for user postgres: 
    psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1), server 10.5)
    Type "help" for help.
    
    postgres=# 
    
©2015-2025 Norria support@alaica.com