如何在 CentOS 8 / RHEL 8 上安装 PostgreSQL

[ad_1]

PostgreSQL 是一个免费的开源对象关系数据库管理系统 (ORDBMS),可用于各种平台,包括 Linux、Microsoft Windows 和 Mac OS X。

PostgreSQL 由 PGDG(PostgreSQL 全球开发组)开发,并在 PostgreSQL 许可下发布,这是一种免费的开源软件。

在这篇文章中,我们将看到如何在 CentOS 8 / RHEL 8 上安装 PostgreSQL。

本文档也适用于 CentOS 7 Debian 10 Debian 9 Ubuntu 18.04 Ubuntu 16.04

在 CentOS 8 / RHEL 8 上安装 PostgreSQL

您可以通过两种方式获取 CentOS 8 / RHEL 8 的 PostgreSQL 包。

从 AppStream 存储库安装 PostgreSQL

PostgreSQL 现在可用于来自 Red Hat 的 Application Stream (rhel-8-for-x86_64-appstream-rpms) 的 RHEL 8 和来自 AppStream 存储库的 CentOS 8。 目前,AppStream 存储库中仅提供 PostgreSQL 10 和 PostgreSQL 9.6。

[[email protected] ~]# yum module list postgresql
Last metadata expiration check: 0:05:18 ago on Wed 25 Sep 2019 06:04:22 AM EDT.
CentOS-8 - AppStream
Name         Stream   Profiles             Summary
postgresql   10 [d]   client, server [d]   PostgreSQL server and client module
postgresql   9.6      client, server [d]   PostgreSQL server and client module

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

安装 PostgreSQL

使用 yum 命令安装 PostgreSQL。

### PostgreSQL 10 ###

yum install -y @postgresql

### PostgreSQL 9.6 ###

yum install -y @postgresql:9.6

安装PostgreSQL后,第一次使用前要先初始化数据库。

postgresql-setup --initdb

输出:

 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log

通常可以找到 PostgreSQL 数据 /var/lib/pgsql/data 目录。

管理 PostgreSQL 服务

要启动 PostgreSQL 服务,请运行:

systemctl start postgresql

要在系统启动时启用 PostgreSQL,请运行:

systemctl enable postgresql

要检查 PostgreSQL 服务的状态,请运行:

systemctl status postgresql

输出:

配置 PostgreSQL 服务器

默认情况下,PostgreSQL 侦听本地主机。 默认情况下,只有在服务器本身上运行的应用程序才能连接到数据库服务器,并限制外部应用程序连接到数据库。

要为外部机器启用数据库服务访问,请编辑配置文件。

vi /var/lib/pgsql/data/postgresql.conf

设置 监听地址*.

listen_addresses="*"

重启 PostgreSQL 服务。

systemctl restart postgresql

使用以下命令确认 PostgreSQL 在所有网络接口上的端口 5432 上侦听 网络统计 命令。

netstat -antup | grep 5432

输出:

tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      10867/postmaster
tcp6       0      0 :::5432                 :::*                    LISTEN      10867/postmaster

从官方 PostgreSQL 存储库安装 PostgreSQL

PostgreSQL 社区优惠 PostgreSQL 包 适用于 CentOS 8 / RHEL 8。PostgreSQL 提供的软件包始终是最新的,并受到 PostgreSQL 社区的支持。

添加 PostgreSQL 存储库

使用以下命令在您的系统上安装 PostgreSQL 存储库配置。

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

安装 PostgreSQL

使用 yum 命令通过提及其版本来安装 PostgreSQL 服务器 postgresql-服务器. 对于这个演示,我正在安装 PostgreSQL 12。

我们需要禁用 rhel-8-for-x86_64-appstream-rpms应用程序流 分别在 RHEL 8 和 CentOS 8 上临时存储库,以允许 yum 从 PostgreSQL 镜像下载包。

### CentOS 8 ###

yum install -y python2-libs
yum --disablerepo=AppStream install -y postgresql12-server postgresql12

### RHEL 8 ###

yum --disablerepo=rhel-8-for-x86_64-appstream-rpms install -y postgresql12-server postgresql12

安装PostgreSQL后,第一次使用前要先初始化数据库。

/usr/pgsql-12/bin/postgresql-12-setup initdb

输出:

Initializing database ... OK

通常可以找到 PostgreSQL 数据 /var/lib/pgsql/12/data/ 目录。

管理 PostgreSQL 服务

要启动 PostgreSQL 服务,请运行:

systemctl start postgresql-12

要在系统启动时启用 PostgreSQL,请运行:

systemctl enable postgresql-12

要检查 PostgreSQL 服务的状态,请运行:

systemctl status postgresql-12

输出:

配置 PostgreSQL 服务器

默认情况下,PostgreSQL 侦听本地主机。 默认情况下,只有在服务器本身上运行的应用程序才能连接到数据库服务器,并限制外部应用程序连接到数据库。

要为外部机器启用数据库服务访问,请编辑配置文件。

vi /var/lib/pgsql/12/data/postgresql.conf

设置 监听地址*.

listen_addresses="*"

重启 PostgreSQL 服务。

systemctl restart postgresql-12

使用以下命令确认 PostgreSQL 在所有网络接口上的端口 5432 上侦听 网络统计 命令。

netstat -antup | grep 5432

输出:

tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      4156/postmaster     
tcp6       0      0 :::5432                 :::*                    LISTEN      4156/postmaster

访问 PostgreSQL 服务器

要访问或使用数据库,请以以下身份登录 邮局 (Linux 用户)用户。 以root用户登录或重置postgres用户密码登录。

# su -l postgres

使用 psql 命令访问数据库。

$ psql

输出:

[[email protected] ~]$ psql
psql (12.0)
Type "help" for help.

postgres=#

为 postgres(数据库管理员)用户设置密码。

postgres=# password

结论

在这篇文章中,您已经学习了如何在 CentOS 8 / RHEL 8 上安装 PostgreSQL。请在评论部分分享您的反馈。

[ad_2]

Related Posts