在 CentOS 8 上安装 PostgreSQL
PostgreSQL 或 Postgres 是一个开放源代码的通用对象关系数据库管理系统,具有许多高级功能,使您可以构建容错环境或复杂的应用程序。
在本指南中,我们将讨论如何在 CentOS 8 上安装 PostgreSQL 数据库服务器。
在选择要安装的版本之前,请确保您的应用程序支持它。我们还将探讨 PostgreSQL 数据库管理的基础。
先决条件
为了能够安装软件包,您需要以 root 用户或具有 sudo 特权的用户身份登录。
在 CentOS 8 上安装 PostgreSQL
在撰写本文时,有两个版本的 PostgreSQL 服务器可从标准 CentOS 存储库中安装:版本 9.6 和 10.0 。
要列出可用的 PostgreSQL 模块,请输入:
dnf module list postgresql
输出显示 postgresql 有两个可用的模块。每个模块都有两个配置文件:服务器和客户端。带有配置文件服务器的 10 是默认设置:
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
-
要安装默认版本, PostgreSQL 服务器版本 10.0 键入:
sudo dnf install @ postgresql:10
-
要安装 PostgreSQL 服务器 9.6 版,请输入:
sudo dnf install @ postgresql:9.6
您可能还需要安装 contrib 软件包,该软件包为 PostgreSQL 数据库提供了一些附加功能:
sudo dnf install postgresql-contrib
安装完成后,使用以下命令初始化 PostgreSQL 数据库:
sudo postgresql-setup initdb
Initializing database ... OK
启动 PostgreSQL 服务,并使其能够在启动时启动:
sudo systemctl enable --now postgresql
使用 psql
工具连接到 PostgreSQL 数据库服务器来验证安装并打印其版本:
sudo -u postgres psql -c "SELECT version();"
PostgreSQL 10.6 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 8.2.1 20180905 (Red Hat 8.2.1-3), 64-bit
PostgreSQL 角色和验证方法
PostgreSQL 使用角色的概念来处理数据库访问权限。角色可以代表一个数据库用户或一组数据库用户。
PostgreSQL 支持多种身份验证方法。最常用的方法是:
- Trust - 使用此方法,只要符合在
pg_hba.conf
中定义的条件,角色就可以在没有密码的情况下进行连接。 - Password - 角色可以通过提供密码进行连接。密码可以存储为
scram-sha-256
,md5
和password
(明文) - Ident - 此方法仅在 TCP/IP 连接上受支持。通过获取客户端的操作系统用户名,使用可选的用户名映射来工作。
- Peer - 与 Ident 相同,但仅在本地连接上支持。
PostgreSQL 客户端身份验证在名为 pg_hba.conf
的配置文件中定义。默认情况下,对于本地连接, PostgreSQL 设置为使用 Peer 身份验证方法。
当您安装 PostgreSQL 服务器会自动创建用户 postgres
。该用户是 PostgreSQL 实例的超级用户。它等效于 MySQL root 用户。
要以 postgres
用户身份登录到 PostgreSQL 服务器,请首先切换到该用户,然后使用该 psql
实用程序访问 PostgreSQL 提示符:
sudo su - postgres
psql
从这里,您可以与 PostgreSQL 实例进行交互。要退出 PostgreSQL shell ,请输入:
\q
您还可以访问 PostgreSQL 提示符,而无需使用以下 sudo
命令切换用户:
sudo -u postgres psql
通常, postgres
仅从本地主机使用该用户。
创建 PostgreSQL 角色和数据库
只有超级用户和具有 CREATEROLE
特权的角色才能创建新角色。
在以下示例中,我们将创建一个名为的新角色 john
,一个名为的数据库 johndb
,并授予该数据库的特权。
-
首先,连接到 PostgreSQL shell :
sudo -u postgres psql
-
使用以下命令创建一个新的 PostgreSQL 角色:
create role john;
-
创建一个新的数据库:
create database johndb;
-
通过运行以下语句,向数据库上的用户授予特权:
grant all privileges on database johndb to john;
启用对 PostgreSQL 服务器的远程访问
默认情况下, PostgreSQL 服务器仅在本地 127.0.0.1
接口上侦听。
要启用对 PostgreSQL 服务器的远程访问,请打开配置文件 /var/lib/pgsql/data/postgresql.conf
:
sudo nano /var/lib/pgsql/data/postgresql.conf
向下滚动到 CONNECTIONS AND AUTHENTICATION
部分,然后添加/编辑以下行:
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
保存文件,并使用以下命令重新启动 PostgreSQL 服务:
sudo systemctl restart postgresql
使用 ss
实用程序验证更改:
ss -nlt | grep 5432
LISTEN 0 128 0.0.0.0:5432 0.0.0.0:*
LISTEN 0 128 [::]:5432 [::]:*
上面的输出显示 PostgreSQL 服务器正在所有接口 (0.0.0.0) 的默认端口上侦听。
最后一步是通过编辑 /var/lib/pgsql/data/pg_hba.conf
配置文件将服务器配置为接受远程连接。
以下是显示不同用例的一些示例:
# TYPE DATABASE USER ADDRESS METHOD
# The user jane can access all databases from all locations using an md5 password
host all jane 0.0.0.0/0 md5
# The user jane can access only the janedb database from all locations using an md5 password
host janedb jane 0.0.0.0/0 md5
# The user jane can access all databases from a trusted location (192.168.1.134) without a password
host all jane 192.168.1.134 trust
结论
CentOS 8 提供了两个 PostgreSQL 版本: 9.6 和 10.0 。
有关此主题的更多信息,请访问 PostgreSQL 文档。