PostgreSQL或Postgres是开放源代码的通用对象关系数据库管理系统,具有许多高级功能,使您可以构建容错环境或复杂的应用程序。
在本指南中,我们将讨论如何在CentOS 8上安装PostgreSQL数据库服务器。在选择要安装的版本之前,请确保您的应用程序支持它。
我们还将探讨PostgreSQL数据库管理的基础。
先决条件
为了能够安装软件包,您需要以root或具有sudo特权的用户身份登录。
在CentOS 8上安装PostgreSQL
在撰写本文时,可以从标准CentOS存储库中安装两个版本的PostgreSQL服务器:版本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
sudo dnf install @postgresql:10
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支持多种身份验证方法。最常用的方法是:
PostgreSQL客户端身份验证在名为的配置文件中定义pg_hba.conf。默认情况下,对于本地连接,PostgreSQL设置为使用对等身份验证方法。
postgres在安装PostgreSQL服务器时会自动创建该用户。该用户是PostgreSQL实例的超级用户。它等效于MySQL root用户。
要以postgres用户身份登录PostgreSQL服务器,请首先切换到该用户,然后使用该psql实用程序访问PostgreSQL提示符:
sudo su - postgrespsql
从这里,您可以与PostgreSQL实例进行交互。要退出PostgreSQL shell,请输入:
\q
您还可以访问PostgreSQL提示符,而无需使用以下sudo命令切换用户:
sudo -u postgres psql
通常,postgres仅从本地主机使用该用户。
创建PostgreSQL角色和数据库
只有超级用户和具有CREATEROLE特权的角色才能创建新角色。
在以下示例中,我们将创建一个名为的新角色john,一个名为的数据库johndb,并授予该数据库的特权。
sudo -u postgres psql
create role john;
复制
create database johndb;
复制
grant all privileges on database johndb to john;
复制
启用对PostgreSQL服务器的远程访问
默认情况下,PostgreSQL服务器仅在本地接口上侦听127.0.0.1。
要启用对PostgreSQL服务器的远程访问,请打开配置文件:
sudo nano /var/lib/pgsql/data/postgresql.conf
向下滚动到该CONNECTIONS AND AUTHENTICATION部分,然后添加/编辑以下行:
/var/lib/pgsql/data/postgresql.conf
#------------------------------------------------------------------------------
# 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)的默认端口上侦听。
最后一步是通过编辑pg_hba.conf文件将服务器配置为接受远程连接。
以下是显示不同用例的一些示例:
/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文档。