Red Hat Enterprise Linux 9 Configuring and using database servers
Red Hat Enterprise Linux 9 Configuring and using database servers
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons
Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is
available at
http://creativecommons.org/licenses/by-sa/3.0/
. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must
provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert,
Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift,
Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States
and other countries.
Linux ® is the registered trademark of Linus Torvalds in the United States and other countries.
XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States
and/or other countries.
MySQL ® is a registered trademark of MySQL AB in the United States, the European Union and
other countries.
Node.js ® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the
official Joyent Node.js open source or commercial project.
The OpenStack ® Word Mark and OpenStack logo are either registered trademarks/service marks
or trademarks/service marks of the OpenStack Foundation, in the United States and other
countries and are used with the OpenStack Foundation's permission. We are not affiliated with,
endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
Abstract
Install the MariaDB, MySQL, or PostgreSQL database server on Red Hat Enterprise Linux 9.
Configure the chosen database server, back up your data, and migrate your data to a later version
of the database server.
Table of Contents
Table of Contents
. . . . . . . . . . . . . FEEDBACK
PROVIDING . . . . . . . . . . . . ON
. . . .RED
. . . . .HAT
. . . . .DOCUMENTATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4. . . . . . . . . . . . .
. . . . . . . . . . . 1.. .INTRODUCTION
CHAPTER . . . . . . . . . . . . . . . . . TO
. . . .DATABASE
. . . . . . . . . . . .SERVERS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5. . . . . . . . . . . . .
.CHAPTER
. . . . . . . . . . 2.
. . USING
. . . . . . . .MARIADB
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6. . . . . . . . . . . . .
2.1. INSTALLING MARIADB 6
2.1.1. Running multiple MariaDB versions in containers 7
2.2. CONFIGURING MARIADB 8
2.3. SETTING UP TLS ENCRYPTION ON A MARIADB SERVER 8
2.3.1. Placing the CA certificate, server certificate, and private key on the MariaDB server 8
2.3.2. Configuring TLS on a MariaDB server 9
2.3.3. Requiring TLS encrypted connections for specific user accounts 11
2.4. GLOBALLY ENABLING TLS ENCRYPTION IN MARIADB CLIENTS 12
2.4.1. Configuring the MariaDB client to use TLS encryption by default 12
2.5. BACKING UP MARIADB DATA 13
2.5.1. Performing logical backup with mariadb-dump 14
2.5.2. Performing physical online backup using the Mariabackup utility 15
2.5.3. Restoring data using the Mariabackup utility 16
2.5.4. Performing file system backup 17
2.5.5. Replication as a backup solution 18
2.6. MIGRATING TO MARIADB 10.5 18
2.6.1. Notable differences between MariaDB 10.3 and MariaDB 10.5 18
2.6.2. Migrating from a RHEL 8 version of MariaDB 10.3 to a RHEL 9 version of MariaDB 10.5 20
2.7. UPGRADING FROM MARIADB 10.5 TO MARIADB 10.11 21
2.7.1. Notable differences between MariaDB 10.5 and MariaDB 10.11 21
2.7.2. Upgrading from a RHEL 9 version of MariaDB 10.5 to MariaDB 10.11 22
2.8. REPLICATING MARIADB WITH GALERA 23
2.8.1. Introduction to MariaDB Galera Cluster 23
2.8.2. Components to build MariaDB Galera Cluster 24
2.8.3. Deploying MariaDB Galera Cluster 24
2.8.4. Adding a new node to MariaDB Galera Cluster 26
2.8.5. Restarting MariaDB Galera Cluster 26
2.9. DEVELOPING MARIADB CLIENT APPLICATIONS 27
. . . . . . . . . . . 3.
CHAPTER . . USING
. . . . . . . .MYSQL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
..............
3.1. INSTALLING MYSQL 28
3.1.1. Running multiple MySQL and MariaDB versions in containers 28
3.2. CONFIGURING MYSQL 30
3.3. SETTING UP TLS ENCRYPTION ON A MYSQL SERVER 30
3.3.1. Placing the CA certificate, server certificate, and private key on the MySQL server 31
3.3.2. Configuring TLS on a MySQL server 32
3.3.3. Requiring TLS encrypted connections for specific user accounts 33
3.4. GLOBALLY ENABLING TLS ENCRYPTION WITH CA CERTIFICATE VALIDATION IN MYSQL CLIENTS 34
3.4.1. Configuring the MySQL client to use TLS encryption by default 34
3.5. BACKING UP MYSQL DATA 35
3.5.1. Performing logical backup with mysqldump 36
3.5.2. Performing file system backup 37
3.5.3. Replication as a backup solution 38
3.6. MIGRATING TO A RHEL 9 VERSION OF MYSQL 8.0 38
3.7. REPLICATING MYSQL 39
3.7.1. Configuring a MySQL source server 40
3.7.2. Configuring a MySQL replica server 40
1
Red Hat Enterprise Linux 9 Configuring and using database servers
.CHAPTER
. . . . . . . . . . 4.
. . .USING
. . . . . . .POSTGRESQL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
..............
4.1. INSTALLING POSTGRESQL 45
4.1.1. Running multiple PostgreSQL versions in containers 46
4.2. CREATING POSTGRESQL USERS 47
4.3. CONFIGURING POSTGRESQL 50
4.4. CONFIGURING TLS ENCRYPTION ON A POSTGRESQL SERVER 51
4.5. BACKING UP POSTGRESQL DATA 57
4.5.1. Backing up PostgreSQL data with an SQL dump 57
4.5.1.1. Advantages and disadvantages of an SQL dump 57
4.5.1.2. Performing an SQL dump using pg_dump 58
4.5.1.3. Performing an SQL dump using pg_dumpall 58
4.5.1.4. Restoring a database dumped using pg_dump 59
4.5.1.5. Restoring databases dumped using pg_dumpall 59
4.5.1.6. Performing an SQL dump of a database on another server 59
4.5.1.7. Handling SQL errors during restore 60
4.5.1.8. Additional resources 60
4.5.2. Backing up PostgreSQL data with a file system level backup 60
4.5.2.1. Advantages and limitations of file system backing up 61
4.5.2.2. Performing file system level backing up 61
4.5.3. Backing up PostgreSQL data by continuous archiving 61
4.5.3.1. Introduction to continuous archiving 61
4.5.3.2. Advantages and disadvantages of continuous archiving 62
4.5.3.3. Setting up WAL archiving 63
4.5.3.4. Making a base backup 64
4.5.3.5. Restoring the database using a continuous archive backup 65
4.5.3.6. Additional resources 67
4.6. MIGRATING TO A RHEL 9 VERSION OF POSTGRESQL 67
4.6.1. Notable differences between PostgreSQL 15 and PostgreSQL 16 67
4.6.2. Notable differences between PostgreSQL 13 and PostgreSQL 15 67
4.6.3. Fast upgrade using the pg_upgrade utility 68
4.6.4. Dump and restore upgrade 70
2
Table of Contents
3
Red Hat Enterprise Linux 9 Configuring and using database servers
4. Enter your suggestion for improvement in the Description field. Include links to the relevant
parts of the documentation.
4
CHAPTER 1. INTRODUCTION TO DATABASE SERVERS
Red Hat Enterprise Linux 9 provides the following database management systems:
MariaDB 10.5
MySQL 8.0
PostgreSQL 13
Redis 6
5
Red Hat Enterprise Linux 9 Configuring and using database servers
Learn how to install and configure MariaDB on a RHEL system, how to back up MariaDB data, how to
migrate from an earlier MariaDB version, and how to replicate a database using the MariaDB Galera
Cluster.
NOTE
By design, it is impossible to install more than one version (stream) of the same module in
parallel. Therefore, you must choose only one of the available streams from the mariadb
module. You can use different versions of the MariaDB database server in containers,
see Running multiple MariaDB versions in containers .
The MariaDB and MySQL database servers cannot be installed in parallel in RHEL 9 due
to conflicting RPM packages. You can use the MariaDB and MySQL database servers in
parallel in containers, see Running multiple MySQL and MariaDB versions in containers .
Procedure
b. For MariaDB 10.11 by selecting stream (version) 11 from the mariadb module and
specifying the server profile, for example:
6
CHAPTER 2. USING MARIADB
Prerequisites
Procedure
1. Use your Red Hat Customer Portal account to authenticate to the registry.redhat.io registry:
Skip this step if you are already logged in to the container registry.
For more information about the usage of this container image, see the Red Hat Ecosystem
Catalog.
For more information about the usage of this container image, see the Red Hat Ecosystem
Catalog.
NOTE
The container names and host ports of the two database servers must differ.
4. To ensure that clients can access the database server on the network, open the host ports in
the firewall:
Verification
$ podman ps
7
Red Hat Enterprise Linux 9 Configuring and using database servers
Additional resources
Procedure
1. Edit the [mysqld] section of the /etc/my.cnf.d/mariadb-server.cnf file. You can set the
following configuration directives:
bind-address - is the address on which the server listens. Possible options are:
a host name
an IPv4 address
an IPv6 address
skip-networking - controls whether the server listens for TCP/IP connections. Possible
values are:
2.3.1. Placing the CA certificate, server certificate, and private key on the MariaDB
server
Before you can enable TLS encryption in the MariaDB server, store the certificate authority (CA)
certificate, the server certificate, and the private key on the MariaDB server.
Prerequisites
The following files in Privacy Enhanced Mail (PEM) format have been copied to the server:
8
CHAPTER 2. USING MARIADB
For details about creating a private key and certificate signing request (CSR), as well as about
requesting a certificate from a CA, see your CA’s documentation.
Procedure
# mv <path>/server.example.com.crt.pem /etc/pki/tls/certs/
# mv <path>/ca.crt.pem /etc/pki/tls/certs/
2. Set permissions on the CA and server certificate that enable the MariaDB server to read the
files:
Because certificates are part of the communication before a secure connection is established,
any client can retrieve them without authentication. Therefore, you do not need to set strict
permissions on the CA and server certificate files.
# mv <path>/server.example.com.key.pem /etc/pki/tls/private/
If unauthorized users have access to the private key, connections to the MariaDB server are no
longer secure.
Prerequisites
The following files in Privacy Enhanced Mail (PEM) format exist on the server and are readable
by the mysql user:
9
Red Hat Enterprise Linux 9 Configuring and using database servers
The subject distinguished name (DN) or the subject alternative name (SAN) field in the server
certificate matches the server’s host name.
Procedure
a. Add the following content to configure the paths to the private key, server and CA
certificate:
[mariadb]
ssl_key = /etc/pki/tls/private/server.example.com.key.pem
ssl_cert = /etc/pki/tls/certs/server.example.com.crt.pem
ssl_ca = /etc/pki/tls/certs/ca.crt.pem
b. If you have a Certificate Revocation List (CRL), configure the MariaDB server to use it:
ssl_crl = /etc/pki/tls/certs/example.crl.pem
c. Optional: Reject connection attempts without encryption. To enable this feature, append:
require_secure_transport = on
d. Optional: Set the TLS versions the server should support. For example, to support TLS 1.2
and TLS 1.3, append:
tls_version = TLSv1.2,TLSv1.3
By default, the server supports TLS 1.1, TLS 1.2, and TLS 1.3.
Verification
To simplify troubleshooting, perform the following steps on the MariaDB server before you configure
the local client to use TLS encryption:
2. If you configured the MariaDB service to only support specific TLS versions, display the
10
CHAPTER 2. USING MARIADB
2. If you configured the MariaDB service to only support specific TLS versions, display the
tls_version variable:
Additional resources
Placing the CA certificate, server certificate, and private key on the MariaDB server
If you cannot configure on the server that a secure transport is required for all connections
(require_secure_transport = on), configure individual user accounts to require TLS encryption.
Prerequisites
Procedure
If your administrative user has no permissions to access the server remotely, perform the
command on the MariaDB server and connect to localhost.
2. Use the REQUIRE SSL clause to enforce that a user must connect using a TLS-encrypted
connection:
Verification
If no error is shown and you have access to the interactive MariaDB console, the connection
with TLS succeeds.
11
Red Hat Enterprise Linux 9 Configuring and using database servers
The server rejected the login attempt because TLS is required for this user but disabled (--skip-
ssl).
Additional resources
Prerequisites
If the certificate authority (CA) that issued the server’s certificate is not trusted by RHEL, the
CA certificate has been copied to the client.
If the MariaDB server runs RHEL 9.2 or later and the FIPS mode is enabled, this client supports
the Extended Master Secret (EMS) extension or uses TLS 1.3. TLS 1.2 connections without EMS
fail. For more information, see the TLS extension "Extended Master Secret" enforced
Knowledgebase article.
Procedure
1. If RHEL does not trust the CA that issued the server’s certificate:
# cp <path>/ca.crt.pem /etc/pki/ca-trust/source/anchors/
b. Set permissions that enable all users to read the CA certificate file:
# update-ca-trust
12
CHAPTER 2. USING MARIADB
[client-mariadb]
ssl
ssl-verify-server-cert
These settings define that the MariaDB client uses TLS encryption ( ssl) and that the client
compares the hostname with the CN in the server certificate (ssl-verify-server-cert).
Verification
Connect to the server using the hostname, and display the server status:
If the SSL entry contains Cipher in use is…, the connection is encrypted.
Note that the user you use in this command has permissions to authenticate remotely.
If the hostname you connect to does not match the hostname in the TLS certificate of the
server, the ssl-verify-server-cert parameter causes the connection to fail. For example, if you
connect to localhost:
Additional resources
Logical backup
Physical backup
Logical backup consists of the SQL statements necessary to restore the data. This type of backup
exports information and records in plain text files.
The main advantage of logical backup over physical backup is portability and flexibility. The data can be
restored on other hardware configurations, MariaDB versions or Database Management System
(DBMS), which is not possible with physical backups.
Note that logical backup can be performed if the mariadb.service is running. Logical backup does not
include log and configuration files.
Physical backup consists of copies of files and directories that store the content.
13
Red Hat Enterprise Linux 9 Configuring and using database servers
Note that physical backup must be performed when the mariadb.service is not running or all tables in
the database are locked to prevent changes during the backup.
You can use one of the following MariaDB backup approaches to back up data from a MariaDB
database:
To perform the mariadb-dump backup, you can use one of the following options:
Procedure
To load one or more dumped full databases back into a server, run:
14
CHAPTER 2. USING MARIADB
To dump a subset of tables from one database, add a list of the chosen tables at the end of the
mariadb-dump command:
NOTE
$ mariadb-dump --help
Additional resources
Mariabackup supports full backup capability for MariaDB server, which includes encrypted and
compressed data.
Prerequisites
You must provide Mariabackup with credentials for the user under which the backup will be run.
You can provide the credentials either on the command line or by a configuration file.
Users of Mariabackup must have the RELOAD, LOCK TABLES, and REPLICATION CLIENT
privileges.
Procedure
15
Red Hat Enterprise Linux 9 Configuring and using database servers
The target-dir option defines the directory where the backup files are stored. If you want to
perform a full backup, the target directory must be empty or not exist.
The user and password options allow you to configure the user name and the password.
2. Add the following lines into the [xtrabackup] or [mysqld] section of the new file:
[xtrabackup]
user=myuser
password=mypassword
Additional resources
--move-back moves the backup files to the data directory and removes the original backup
files.
To restore data using the Mariabackup utility, use the following procedure.
Prerequisites
Users of Mariabackup must have the RELOAD, LOCK TABLES, and REPLICATION CLIENT
privileges.
Procedure
To restore data and keep the original backup files, use the --copy-back option:
16
CHAPTER 2. USING MARIADB
To restore data and remove the original backup files, use the --move-back option:
For example, to recursively change ownership of the files to the mysql user and group:
Additional resources
To back up also your current configuration or the log files, use the optional steps of the following
procedure.
Procedure
# cp -r /var/lib/mysql /backup-location
# cp /var/log/mariadb/* /backup-location/logs
6. When loading the backed up data from the backup location to the /var/lib/mysql directory,
17
Red Hat Enterprise Linux 9 Configuring and using database servers
6. When loading the backed up data from the backup location to the /var/lib/mysql directory,
ensure that mysql:mysql is an owner of all data in /var/lib/mysql:
WARNING
Additional resources
This part describes migration from a RHEL 8 version of MariaDB 10.3 to RHEL 9 version of MariaDB
10.5.
MariaDB now uses the unix_socket authentication plugin by default. The plugin enables users
to use operating system credentials when connecting to MariaDB through the local UNIX
socket file.
MariaDB adds mariadb-* named binaries and mysql* symbolic links pointing to the mariadb-*
binaries. For example, the mysqladmin, mysqlaccess, and mysqlshow symlinks point to the
mariadb-admin, mariadb-access, and mariadb-show binaries, respectively.
The SUPER privilege has been split into several privileges to better align with each user role. As
a result, certain statements have changed required privileges.
In the InnoDB storage engine, defaults of the following variables have been changed:
18
CHAPTER 2. USING MARIADB
In the InnoDB storage engine, defaults of the following variables have been changed:
innodb_adaptive_hash_index to OFF and innodb_checksum_algorithm to full_crc32.
MariaDB now uses the libedit implementation of the underlying software managing the
MariaDB command history (the .mysql_history file) instead of the previously used readline
library. This change impacts users working directly with the .mysql_history file. Note that
.mysql_history is a file managed by the MariaDB or MySQL applications, and users should not
work with the file directly. The human-readable appearance is coincidental.
NOTE
To increase security, you can consider not maintaining a history file. To disable
the command history recording:
$ ln -s /dev/null $HOME/.mysql_history
MariaDB Galera Cluster has been upgraded to version 4 with the following notable changes:
Galera adds a new streaming replication feature, which supports replicating transactions of
unlimited size. During an execution of streaming replication, a cluster replicates a transaction in
small fragments.
The default value for the wsrep_on option in the /etc/my.cnf.d/galera.cnf file has changed
from 1 to 0 to prevent end users from starting wsrep replication without configuring required
additional options.
MariaDB 10.5 adds a new version of the Pluggable Authentication Modules (PAM) plugin. The
PAM plugin version 2.0 performs PAM authentication using a separate setuid root helper
binary, which enables MariaDB to use additional PAM modules.
The helper binary can be executed only by users in the mysql group. By default, the group
contains only the mysql user. Red Hat recommends that administrators do not add more users
to the mysql group to prevent password-guessing attacks without throttling or logging through
this helper utility.
In MariaDB 10.5, the Pluggable Authentication Modules (PAM) plugin and its related files have
been moved to a new package, mariadb-pam. As a result, no new setuid root binary is
introduced on systems that do not use PAM authentication for MariaDB.
The mariadb-pam package contains both PAM plugin versions: version 2.0 is the default, and
version 1.0 is available as the auth_pam_v1 shared object library.
The mariadb-pam package is not installed by default with the MariaDB server. To make the
19
Red Hat Enterprise Linux 9 Configuring and using database servers
The mariadb-pam package is not installed by default with the MariaDB server. To make the
PAM authentication plugin available in MariaDB 10.5, install the mariadb-pam package
manually.
Prerequisites
Before performing the upgrade, back up all your data stored in the MariaDB databases.
Procedure
2. Ensure that the mariadb service is not running on either of the source and target systems at
the time of copying data:
3. Copy the data from the source location to the /var/lib/mysql/ directory on the RHEL 9 target
system.
4. Set the appropriate permissions and SELinux context for copied files on the target system:
6. Adjust the configuration so that option files located in /etc/my.cnf.d/ include only options valid
for MariaDB 10.5. For details, see upstream documentation for MariaDB 10.4 and MariaDB 10.5.
# galera_new_cluster
20
CHAPTER 2. USING MARIADB
$ mariadb-upgrade
$ mariadb-upgrade --skip-write-binlog
IMPORTANT
There are certain risks and known problems related to an in-place upgrade. For example,
some queries might not work or they will be run in a different order than before the
upgrade. For more information about these risks and problems, and for general
information about an in-place upgrade, see MariaDB 10.5 Release Notes .
The CREATE TABLE, ALTER TABLE, RENAME TABLE, DROP TABLE, DROP DATABASE,
and related Data Definition Language (DDL) statements are now atomic. The statement must
be fully completed, otherwise the changes are reverted. Note that when deleting multiple tables
with DROP TABLE, only each individual drop is atomic, not the full list of tables.
The SUPER and READ ONLY ADMIN privileges are now separate.
You can now store universally unique identifiers in the new UUID database data type.
MariaDB now supports the Secure Socket Layer (SSL) protocol version 3.
The MariaDB server now requires correctly configured SSL to start. Previously, MariaDB
silently disabled SSL and used insecure connections in case of misconfigured SSL.
MariaDB now supports the natural sort order through the natural_sort_key() function.
The utf8 character set (and related collations) is now by default an alias for utf8mb3.
systemd socket activation files for MariaDB are now available in the /usr/share/ directory.
Note that they are not a part of the default configuration in RHEL as opposed to upstream.
21
Red Hat Enterprise Linux 9 Configuring and using database servers
The default logrotate file has changed significantly. Review your configuration before migrating
to MariaDB 10.11.
For MariaDB and MySQL clients, the connection property specified on the command line (for
example, --port=3306), now forces the protocol type of communication between the client and
the server, such as tcp, socket, pipe, or memory. Previously, for example, the specified port was
ignored if a MariaDB client connected through a UNIX socket.
Prerequisites
Before performing the upgrade, back up all your data stored in the MariaDB databases.
Procedure
3. Adjust the configuration so that option files located in /etc/my.cnf.d/ include only options valid
for MariaDB 10.11. For details, see upstream documentation for MariaDB 10.6 and MariaDB 10.11.
# galera_new_cluster
# mariadb-upgrade
22
CHAPTER 2. USING MARIADB
# mariadb-upgrade --skip-write-binlog
IMPORTANT
There are certain risks and known problems related to an in-place upgrade. For example,
some queries might not work or they will be run in a different order than before the
upgrade. For more information about these risks and problems, and for general
information about an in-place upgrade, see MariaDB 10.11 Release Notes .
The interface between Galera replication and a MariaDB database is defined by the write set replication
API (wsrep API).
Synchronous replication
Direct client connections: users can log on to the cluster nodes, and work with the nodes
directly while the replication runs
Synchronous replication means that a server replicates a transaction at commit time by broadcasting
the write set associated with the transaction to every node in the cluster. The client (user application)
connects directly to the Database Management System (DBMS), and experiences behavior that is
similar to native MariaDB.
Synchronous replication guarantees that a change that happened on one node in the cluster happens
on other nodes in the cluster at the same time.
Therefore, synchronous replication has the following advantages over asynchronous replication:
23
Red Hat Enterprise Linux 9 Configuring and using database servers
The latest changes are not lost if one of the cluster nodes crashes
Additional resources
mariadb-server-galera - contains support files and scripts for MariaDB Galera Cluster.
mariadb-server - is patched by MariaDB upstream to include the write set replication API
(wsrep API). This API provides the interface between Galera replication and MariaDB.
galera - is patched by MariaDB upstream to add full support for MariaDB. The galera package
contains the following:
The Galera Arbitrator utility can be used as a cluster member that participates in voting in
split-brain scenarios. However, Galera Arbitrator cannot participate in the actual
replication.
Galera Systemd service and Galera wrapper script which are used for deploying the
Galera Arbitrator utility. RHEL 9 provides the upstream version of these files, located at
/usr/lib/systemd/system/garbd.service and /usr/sbin/garb-systemd.
Additional resources
Galera Arbitrator
mysql-wsrep project
Prerequisites
24
CHAPTER 2. USING MARIADB
As a result, the following packages are installed together with their dependencies:
mariadb-server-galera
mariadb-server
galera
For more information about these packages reqired to build MariaDB Galera Cluster, see
Components to build MariaDB Cluster .
The MariaDB server replication configuration must be updated before the system is added to a
cluster for the first time.
The default configuration is distributed in the /etc/my.cnf.d/galera.cnf file.
Before deploying MariaDB Galera Cluster, set the wsrep_cluster_address option in the
/etc/my.cnf.d/galera.cnf file on all nodes to start with the following string:
gcomm://
wsrep_cluster_address="gcomm://"
For all other nodes, set wsrep_cluster_address to include an address to any node which is
already a part of the running cluster. For example:
wsrep_cluster_address="gcomm://10.0.0.10"
For more information about how to set Galera Cluster address, see Galera Cluster Address .
Procedure
1. Bootstrap a first node of a new cluster by running the following wrapper on that node:
# galera_new_cluster
This wrapper ensures that the MariaDB server daemon (mariadbd) runs with the --wsrep-new-
cluster option. This option provides the information that there is no existing cluster to connect
to. Therefore, the node creates a new UUID to identify the new cluster.
NOTE
The mariadb service supports a systemd method for interacting with multiple
MariaDB server processes. Therefore, in cases with multiple running MariaDB
servers, you can bootstrap a specific instance by specifying the instance name as
a suffix:
# galera_new_cluster mariadb@node1
2. Connect other nodes to the cluster by running the following command on each of the nodes:
25
Red Hat Enterprise Linux 9 Configuring and using database servers
As a result, the node connects to the cluster, and synchronizes itself with the state of the
cluster.
Additional resources
Note that you can also use this procedure to reconnect an already existing node.
Procedure
On the particular node, provide an address to one or more existing cluster members in the
wsrep_cluster_address option within the [mariadb] section of the /etc/my.cnf.d/galera.cnf
configuration file :
[mariadb]
wsrep_cluster_address="gcomm://192.168.0.1"
When a new node connects to one of the existing cluster nodes, it is able to see all nodes in the
cluster.
As a result, any node can join a cluster by connecting to any other cluster node, even if one or
more cluster nodes are down. When all members agree on the membership, the cluster’s state is
changed. If the new node’s state is different from the state of the cluster, the new node
requests either an Incremental State Transfer (IST) or a State Snapshot Transfer (SST) to
ensure consistency with the other nodes.
Additional resources
To restart the cluster, bootstrap a first node as described in Configuring MariaDB Galera cluster .
26
CHAPTER 2. USING MARIADB
WARNING
If the cluster is not bootstrapped, and mariadbd on the first node is started with
only the systemctl start mariadb command, the node tries to connect to at least
one of the nodes listed in the wsrep_cluster_address option in the
/etc/my.cnf.d/galera.cnf file. If no nodes are currently running, the restart fails.
Additional resources
The development files and programs necessary to build applications against the MariaDB client library
are provided by the mariadb-connector-c-devel package.
Instead of using a direct library name, use the mariadb_config program, which is distributed in the
mariadb-connector-c-devel package. This program ensures that the correct build flags are returned.
27
Red Hat Enterprise Linux 9 Configuring and using database servers
Learn how to install and configure MySQL on a RHEL system, how to back up MySQL data, how to
migrate from an earlier MySQL version, and how to replicate a MySQL.
NOTE
The MySQL and MariaDB database servers cannot be installed in parallel in RHEL 9 due
to conflicting RPM packages. You can use the MySQL and MariaDB database servers in
parallel in containers, see Running multiple MySQL and MariaDB versions in containers .
Procedure
4. Recommended: To improve security when installing MySQL, run the following command:
$ mysql_secure_installation
The command launches a fully interactive script, which prompts for each step in the process.
The script enables you to improve security in the following ways:
To run both MySQL and MariaDB on the same host, run them in containers because you cannot install
28
CHAPTER 3. USING MYSQL
To run both MySQL and MariaDB on the same host, run them in containers because you cannot install
these database servers in parallel due to conflicting RPM packages.
This procedure includes MySQL 8.0 and MariaDB 10.5 as examples but you can use any MySQL or
MariaDB container version available in the Red Hat Ecosystem Catalog.
Prerequisites
Procedure
1. Use your Red Hat Customer Portal account to authenticate to the registry.redhat.io registry:
Skip this step if you are already logged in to the container registry.
For more information about the usage of this container image, see the Red Hat Ecosystem
Catalog.
For more information about the usage of this container image, see the Red Hat Ecosystem
Catalog.
For more information about the usage of this container image, see the Red Hat Ecosystem
Catalog.
NOTE
The container names and host ports of the two database servers must differ.
5. To ensure that clients can access the database server on the network, open the host ports in
the firewall:
29
Red Hat Enterprise Linux 9 Configuring and using database servers
Verification
$ podman ps
Additional resources
Procedure
1. Edit the [mysqld] section of the /etc/my.cnf.d/mysql-server.cnf file. You can set the following
configuration directives:
bind-address - is the address on which the server listens. Possible options are:
a host name
an IPv4 address
an IPv6 address
skip-networking - controls whether the server listens for TCP/IP connections. Possible
values are:
By default, MySQL uses unencrypted connections. For secure connections, enable TLS support on the
30
CHAPTER 3. USING MYSQL
By default, MySQL uses unencrypted connections. For secure connections, enable TLS support on the
MySQL server and configure your clients to establish encrypted connections.
3.3.1. Placing the CA certificate, server certificate, and private key on the MySQL
server
Before you can enable TLS encryption on the MySQL server, store the certificate authority (CA)
certificate, the server certificate, and the private key on the MySQL server.
Prerequisites
The following files in Privacy Enhanced Mail (PEM) format have been copied to the server:
For details about creating a private key and certificate signing request (CSR), as well as about
requesting a certificate from a CA, see your CA’s documentation.
Procedure
# mv <path>/server.example.com.crt.pem /etc/pki/tls/certs/
# mv <path>/ca.crt.pem /etc/pki/tls/certs/
2. Set permissions on the CA and server certificate that enable the MySQL server to read the
files:
Because certificates are part of the communication before a secure connection is established,
any client can retrieve them without authentication. Therefore, you do not need to set strict
permissions on the CA and server certificate files.
# mv <path>/server.example.com.key.pem /etc/pki/tls/private/
If unauthorized users have access to the private key, connections to the MySQL server are no
longer secure.
31
Red Hat Enterprise Linux 9 Configuring and using database servers
Prerequisites
The following files in Privacy Enhanced Mail (PEM) format exist on the server and are readable
by the mysql user:
The subject distinguished name (DN) or the subject alternative name (SAN) field in the server
certificate matches the server’s host name.
Procedure
a. Add the following content to configure the paths to the private key, server and CA
certificate:
[mysqld]
ssl_key = /etc/pki/tls/private/server.example.com.key.pem
ssl_cert = /etc/pki/tls/certs/server.example.com.crt.pem
ssl_ca = /etc/pki/tls/certs/ca.crt.pem
b. If you have a Certificate Revocation List (CRL), configure the MySQL server to use it:
ssl_crl = /etc/pki/tls/certs/example.crl.pem
c. Optional: Reject connection attempts without encryption. To enable this feature, append:
require_secure_transport = on
d. Optional: Set the TLS versions the server should support. For example, to support TLS 1.2
and TLS 1.3, append:
tls_version = TLSv1.2,TLSv1.3
By default, the server supports TLS 1.1, TLS 1.2, and TLS 1.3.
Verification
To simplify troubleshooting, perform the following steps on the MySQL server before you configure the
32
CHAPTER 3. USING MYSQL
To simplify troubleshooting, perform the following steps on the MySQL server before you configure the
local client to use TLS encryption:
2. If you configured the MySQL server to only support specific TLS versions, display the
tls_version variable:
3. Verify that the server uses the correct CA certificate, server certificate, and private key files:
Additional resources
Placing the CA certificate, server certificate, and private key on the MySQL server
If you cannot configure on the server that a secure transport is required for all connections
(require_secure_transport = on), configure individual user accounts to require TLS encryption.
Prerequisites
33
Red Hat Enterprise Linux 9 Configuring and using database servers
Procedure
If your administrative user has no permissions to access the server remotely, perform the
command on the MySQL server and connect to localhost.
2. Use the REQUIRE SSL clause to enforce that a user must connect using a TLS-encrypted
connection:
Verification
If no error is shown and you have access to the interactive MySQL console, the connection with
TLS succeeds.
By default, the client automatically uses TLS encryption if the server provides it. Therefore, the
--ssl-ca=ca.crt.pem and --ssl-mode=VERIFY_IDENTITY options are not required, but improve
the security because, with these options, the client verifies the identity of the server.
The server rejected the login attempt because TLS is required for this user but disabled (--ssl-
mode=DISABLED).
Additional resources
On RHEL, you can globally configure that the MySQL client uses TLS encryption and verifies that the
34
CHAPTER 3. USING MYSQL
On RHEL, you can globally configure that the MySQL client uses TLS encryption and verifies that the
Common Name (CN) in the server certificate matches the hostname the user connects to. This
prevents man-in-the-middle attacks.
Prerequisites
Procedure
[client]
ssl-mode=VERIFY_IDENTITY
ssl-ca=/etc/pki/tls/certs/ca.crt.pem
These settings define that the MySQL client uses TLS encryption and that the client compares
the hostname with the CN in the server certificate (ssl-mode=VERIFY_IDENTITY).
Additionally, it specifies the path to the CA certificate (ssl-ca).
Verification
Connect to the server using the hostname, and display the server status:
If the SSL entry contains Cipher in use is…, the connection is encrypted.
Note that the user you use in this command has permissions to authenticate remotely.
If the hostname you connect to does not match the hostname in the TLS certificate of the
server, the ssl-mode=VERIFY_IDENTITY parameter causes the connection to fail. For
example, if you connect to localhost:
Additional resources
Logical backup
Physical backup
Logical backup consists of the SQL statements necessary to restore the data. This type of backup
35
Red Hat Enterprise Linux 9 Configuring and using database servers
Logical backup consists of the SQL statements necessary to restore the data. This type of backup
exports information and records in plain text files.
The main advantage of logical backup over physical backup is portability and flexibility. The data can be
restored on other hardware configurations, MySQL versions or Database Management System (DBMS),
which is not possible with physical backups.
Note that logical backup can be performed if the mysqld.service is running. Logical backup does not
include log and configuration files.
Physical backup consists of copies of files and directories that store the content.
Note that physical backup must be performed when the mysqld.service is not running or all tables in
the database are locked to prevent changes during the backup.
You can use one of the following MySQL backup approaches to back up data from a MySQL database:
To perform the mysqldump backup, you can use one of the following options:
Procedure
36
CHAPTER 3. USING MYSQL
To load one or more dumped full databases back into a server, run:
To dump a literal,subset of tables from one database, add a list of the chosen tables at the end
of the mysqldump command:
NOTE
$ mysqldump --help
Additional resources
To back up also your current configuration or the log files, use the optional steps of the following
procedure.
Procedure
37
Red Hat Enterprise Linux 9 Configuring and using database servers
# cp -r /var/lib/mysql /backup-location
# cp /var/log/mysql/* /backup-location/logs
6. When loading the backed up data from the backup location to the /var/lib/mysql directory,
ensure that mysql:mysql is an owner of all data in /var/lib/mysql:
WARNING
Additional resources
This procedure describes migration from a RHEL 8 version of MySQL 8.0 to a RHEL 9 version of MySQL
8.0 using the mysql_upgrade utility. The mysql_upgrade utility is provided by the mysql-server
package.
38
CHAPTER 3. USING MYSQL
Prerequisites
Before performing the upgrade, back up all your data stored in the MySQL databases. See
Backing up MySQL data.
Procedure
2. Ensure that the mysqld service is not running on either of the source and target systems at the
time of copying data:
3. Copy the data from the source location to the /var/lib/mysql/ directory on the RHEL 9 target
system.
4. Set the appropriate permissions and SELinux context for copied files on the target system:
Note: In earlier versions of MySQL, the mysql_upgrade command was needed to check and
repair internal tables. This is now done automatically when you start the server.
IMPORTANT
39
Red Hat Enterprise Linux 9 Configuring and using database servers
IMPORTANT
If you want to use existing MySQL servers for replication, you must first synchronize data.
See the upstream documentation for more information.
Prerequisites
Procedure
1. Include the following options in the /etc/my.cnf.d/mysql-server.cnf file under the [mysqld]
section:
bind-address=source_ip_adress
This option is required for connections made from replicas to the source.
server-id=id
The id must be unique.
log_bin=path_to_source_server_log
This option defines a path to the binary log file of the MySQL source server. For example:
log_bin=/var/log/mysql/mysql-bin.log.
gtid_mode=ON
This option enables global transaction identifiers (GTIDs) on the server.
enforce-gtid-consistency=ON
The server enforces GTID consistency by allowing execution of only statements that can be
safely logged using a GTID.
Optional: binlog_do_db=db_name
Use this option if you want to replicate only selected databases. To replicate more than one
selected database, specify each of the databases separately:
binlog_do_db=db_name1
binlog_do_db=db_name2
binlog_do_db=db_name3
Optional: binlog_ignore_db=db_name
Use this option to exclude a specific database from replication.
You can set configuration options required for a MySQL replica server to ensure a successful
40
CHAPTER 3. USING MYSQL
You can set configuration options required for a MySQL replica server to ensure a successful
replication.
Prerequisites
Procedure
1. Include the following options in the /etc/my.cnf.d/mysql-server.cnf file under the [mysqld]
section:
server-id=id
The id must be unique.
relay-log=path_to_replica_server_log
The relay log is a set of log files created by the MySQL replica server during replication.
log_bin=path_to_replica_sever_log
This option defines a path to the binary log file of the MySQL replica server. For example:
log_bin=/var/log/mysql/mysql-bin.log.
gtid_mode=ON
This option enables global transaction identifiers (GTIDs) on the server.
enforce-gtid-consistency=ON
The server enforces GTID consistency by allowing execution of only statements that can be
safely logged using a GTID.
log-replica-updates=ON
This option ensures that updates received from the source server are logged in the replica’s
binary log.
skip-replica-start=ON
This option ensures that the replica server does not start the replication threads when the
replica server starts.
Optional: binlog_do_db=db_name
Use this option if you want to replicate only certain databases. To replicate more than one
database, specify each of the databases separately:
binlog_do_db=db_name1
binlog_do_db=db_name2
binlog_do_db=db_name3
Optional: binlog_ignore_db=db_name
Use this option to exclude a specific database from replication.
41
Red Hat Enterprise Linux 9 Configuring and using database servers
Prerequisites
The source server is installed and configured as described in Configuring a MySQL source
server.
Procedure
Prerequisites
The source server is installed and configured as described in Configuring a MySQL source
server.
The replica server is installed and configured as described in Configuring a MySQL replica
server.
You have created a replication user. See Creating a replication user on the MySQL source
server.
Procedure
42
CHAPTER 3. USING MYSQL
4. Unset the read-only state on both the source and replica servers:
5. Optional: Inspect the status of the replica server for debugging purposes:
NOTE
If the replica server fails to start or connect, you can skip a certain number of
events following the binary log file position displayed in the output of the SHOW
MASTER STATUS command. For example, skip the first event from the defined
position:
3.7.5. Verification
1. Create an example database on the source server:
3. Display status information about the binary log files of the MySQL server by executing the
following command on either of the source or replica servers:
The Executed_Gtid_Set column, which shows a set of GTIDs for transactions executed on the
source, must not be empty.
NOTE
43
Red Hat Enterprise Linux 9 Configuring and using database servers
NOTE
The same set of GTIDs is displayed in the Executed_Gtid_Set row when you use
the SHOW SLAVE STATUS on the replica server.
The development files and programs necessary to build applications against the MariaDB client library
are provided by the mariadb-connector-c-devel package.
Instead of using a direct library name, use the mariadb_config program, which is distributed in the
mariadb-connector-c-devel package. This program ensures that the correct build flags are returned.
44
CHAPTER 4. USING POSTGRESQL
The PostgreSQL server includes features for ensuring data integrity, building fault-tolerant
environments and applications. With the PostgreSQL server, you can extend a database with your own
data types, custom functions, or code from different programming languages without the need to
recompile the database.
Learn how to install and configure PostgreSQL on a RHEL system, how to back up PostgreSQL data,
and how to migrate from an earlier PostgreSQL version.
Additional PostgreSQL versions are provided as modules with a shorter life cycle in minor releases of
RHEL 9:
NOTE
By design, it is impossible to install more than one version (stream) of the same module in
parallel. Therefore, you must choose only one of the available streams from the
postgresql module. You can use different versions of the PostgreSQL database server
in containers, see Running multiple PostgreSQL versions in containers.
Procedure
# postgresql-setup --initdb
45
Red Hat Enterprise Linux 9 Configuring and using database servers
Red Hat recommends storing the data in the default /var/lib/pgsql/data directory.
IMPORTANT
If you want to upgrade from an earlier postgresql stream within RHEL 9, follow both
procedures described in Switching to a later stream and in Migrating to a RHEL 9 version
of PostgreSQL.
This procedure includes PostgreSQL 13 and PostgreSQL 15 as examples but you can use any
PostgreSQL container version available in the Red Hat Ecosystem Catalog.
Prerequisites
Procedure
1. Use your Red Hat Customer Portal account to authenticate to the registry.redhat.io registry:
Skip this step if you are already logged in to the container registry.
For more information about the usage of this container image, see the Red Hat Ecosystem
Catalog.
For more information about the usage of this container image, see the Red Hat Ecosystem
46
CHAPTER 4. USING POSTGRESQL
For more information about the usage of this container image, see the Red Hat Ecosystem
Catalog.
For more information about the usage of this container image, see the Red Hat Ecosystem
Catalog.
NOTE
The container names and host ports of the two database servers must differ.
5. To ensure that clients can access the database server on the network, open the host ports in
the firewall:
Verification
$ podman ps
Additional resources
The postgres UNIX system user - should be used only to run the PostgreSQL server and client
applications, such as pg_dump. Do not use the postgres system user for any interactive work
on PostgreSQL administration, such as database creation and user management.
A database superuser - the default postgres PostgreSQL superuser is not related to the
postgres system user. You can limit access of the postgres superuser in the pg_hba.conf file,
otherwise no other permission limitations exist. You can also create other database superusers.
47
Red Hat Enterprise Linux 9 Configuring and using database servers
Roles can own database objects (for example, tables and functions) and can assign object privileges to
other roles using SQL commands.
Standard database management privileges include SELECT, INSERT, UPDATE, DELETE, TRUNCATE,
REFERENCES, TRIGGER, CREATE, CONNECT, TEMPORARY, EXECUTE, and USAGE.
Role attributes are special privileges, such as LOGIN, SUPERUSER, CREATEDB, and CREATEROLE.
IMPORTANT
Red Hat recommends performing most tasks as a role that is not a superuser. A common
practice is to create a role that has the CREATEDB and CREATEROLE privileges and
use this role for all routine management of databases and roles.
Prerequisites
Procedure
To create a user, set a password for the user, and assign the user the CREATEROLE and
CREATEDB permissions:
Replace mydbuser with the username and mypasswd with the user’s password.
Additional resources
PostgreSQL privileges
Configuring PostgreSQL
This example demonstrates how to initialize a PostgreSQL database, create a database user with
routine database management privileges, and how to create a database that is accessible from any
system account through the database user with management privileges.
48
CHAPTER 4. USING POSTGRESQL
# postgresql-setup --initdb
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
to:
password_encryption = scram-sha-256
b. In the /var/lib/pgsql/data/pg_hba.conf file, change the following line for the IPv4 local
connections:
to:
# su - postgres
$ psql
psql (13.7)
Type "help" for help.
postgres=#
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in
"/var/run/postgresql" at port "5432".
8. Create a user named mydbuser, set a password for mydbuser, and assign mydbuser the
CREATEROLE and CREATEDB permissions:
49
Red Hat Enterprise Linux 9 Configuring and using database servers
The mydbuser user now can perform routine database management operations: create
databases and manage user indexes.
postgres=# \q
$ logout
11. Log in to the PostgreSQL terminal as mydbuser, specify the hostname, and connect to the
default postgres database, which was created during initialization:
postgres=>
postgres=# \q
mydatabase=> \conninfo
You are connected to database "mydatabase" as user "mydbuser" on host "127.0.0.1" at
port "5432".
50
CHAPTER 4. USING POSTGRESQL
pg_ident.conf - is used for mapping user identities from external authentication mechanisms
into the PostgreSQL user identities.
Procedure
This example shows basic settings of the database cluster parameters in the
/var/lib/pgsql/data/postgresql.conf file.
# This is a comment
log_connections = yes
log_destination = 'syslog'
search_path = '"$user", public'
shared_buffers = 128MB
password_encryption = scram-sha-256
Prerequisites
51
Red Hat Enterprise Linux 9 Configuring and using database servers
Prerequisites
If the server runs RHEL 9.2 or later and the FIPS mode is enabled, clients must either support
the Extended Master Secret (EMS) extension or use TLS 1.3. TLS 1.2 connections without EMS
fail. For more information, see the TLS extension "Extended Master Secret" enforced
Knowledgebase article.
Procedure
# openssl req -new -x509 -days 365 -nodes -text -out server.crt \
-keyout server.key -subj "/CN=dbhost.yourdomain.com"
3. Copy your signed certificate and your private key to the required locations on the database
server:
# cp server.{key,crt} /var/lib/pgsql/data/.
4. Change the owner and group ownership of the signed certificate and your private key to the
postgres user:
5. Restrict the permissions for your private key so that it is readable only by the owner:
6. Set the password hashing algorithm to scram-sha-256 by changing the following line in the
/var/lib/pgsql/data/postgresql.conf file:
to:
password_encryption = scram-sha-256
#ssl = off
to:
52
CHAPTER 4. USING POSTGRESQL
ssl=on
8. Restrict access to all databases to accept only connections from clients using TLS by changing
the following line for the IPv4 local connections in the /var/lib/pgsql/data/pg_hba.conf file:
to:
Alternatively, you can restrict access for a single database and a user by adding the following
new line:
Replace mydatabase with the database name and mydbuser with the username.
Verification
1. Connect to the PostgreSQL database as the mydbuser user, specify the hostname and the
database name:
Replace mydatabase with the database name and mydbuser with the username.
mydbuser=> \conninfo
You are connected to database "mydatabase" as user "mydbuser" on host "127.0.0.1" at
port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256,
compression: off)
You can write a simple application that verifies whether a connection to PostgreSQL is
encrypted. This example demonstrates such an application written in C that uses the libpq
client library, which is provided by the libpq-devel package:
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
53
Red Hat Enterprise Linux 9 Configuring and using database servers
if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//Verify TLS
if (PQsslInUse(connection)){
printf("TLS in use\n");
printf("%s\n", PQsslAttribute(connection,"protocol"));
}
//End connection
PQfinish(connection);
printf("Disconnected\n");
return 0;
}
Replace mypassword with the password, mydatabase with the database name, and mydbuser
with the username.
NOTE
You must load the pq libraries for compilation by using the -lpq option. For
example, to compile the application by using the GCC compiler:
where the source_file.c contains the example code above, and myapplication is
the name of your application for verifying secured PostgreSQL connection.
Example 4.4. Initializing, creating, and connecting to a PostgreSQL database using TLS
encryption
This example demonstrates how to initialize a PostgreSQL database, create a database user and a
database, and how to connect to the database using a secured connection.
# postgresql-setup --initdb
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
54
CHAPTER 4. USING POSTGRESQL
# openssl req -new -x509 -days 365 -nodes -text -out server.crt \
-keyout server.key -subj "/CN=dbhost.yourdomain.com"
5. Copy your signed certificate and your private key to the required locations on the database
server:
# cp server.{key,crt} /var/lib/pgsql/data/.
6. Change the owner and group ownership of the signed certificate and your private key to the
postgres user:
7. Restrict the permissions for your private key so that it is readable only by the owner:
to:
password_encryption = scram-sha-256
#ssl = off
to:
ssl=on
# su - postgres
$ psql -U postgres
psql (13.7)
Type "help" for help.
55
Red Hat Enterprise Linux 9 Configuring and using database servers
postgres=#
13. Create a user named mydbuser and set a password for mydbuser:
postgres=# \q
$ logout
18. Restrict access to all databases to accept only connections from clients using TLS by
changing the following line for the IPv4 local connections in the
/var/lib/pgsql/data/pg_hba.conf file:
to:
20. Connect to the PostgreSQL database as the mydbuser user, specify the hostname and the
database name:
56
CHAPTER 4. USING POSTGRESQL
mydatabase=>
SQL dump
See Backing up with SQL dump.
File system level backup
See File system level backup .
Continuous archiving
See Continuous archiving.
pg_dump dumps a single database without cluster-wide information about roles or tablespaces
pg_dumpall dumps each database in a given cluster and preserves cluster-wide data, such as
role and tablespace definitions.
By default, the pg_dump and pg_dumpall commands write their results into the standard output. To
store the dump in a file, redirect the output to an SQL file. The resulting SQL file can be either in a text
format or in other formats that allow for parallelism and for more detailed control of object restoration.
You can perform the SQL dump from any remote host that has access to the database.
An SQL dump has the following advantages compared to other PostgreSQL backup methods:
An SQL dump is the only PostgreSQL backup method that is not server version-specific. The
output of the pg_dump utility can be reloaded into later versions of PostgreSQL, which is not
possible for file system level backups or continuous archiving.
An SQL dump is the only method that works when transferring a database to a different
machine architecture, such as going from a 32-bit to a 64-bit server.
An SQL dump provides internally consistent dumps. A dump represents a snapshot of the
database at the time pg_dump began running.
The pg_dump utility does not block other operations on the database when it is running.
A disadvantage of an SQL dump is that it takes more time compared to file system level backup.
57
Red Hat Enterprise Linux 9 Configuring and using database servers
To dump a single database without cluster-wide information, use the pg_dump utility.
Prerequisites
You must have read access to all tables that you want to dump. To dump the entire database,
you must run the commands as the postgres superuser or a user with database administrator
privileges.
Procedure
To specify which database server pg_dump will contact, use the following command-line options:
To dump each database in a given database cluster and to preserve cluster-wide data, use the
pg_dumpall utility.
Prerequisites
You must run the commands as the postgres superuser or a user with database administrator
privileges.
Procedure
Dump all databases in the database cluster and preserve cluster-wide data:
To specify which database server pg_dumpall will contact, use the following command-line options:
58
CHAPTER 4. USING POSTGRESQL
To restore a database from an SQL dump that you dumped using the pg_dump utility, follow the steps
below.
Prerequisites
You must run the commands as the postgres superuser or a user with database administrator
privileges.
Procedure
$ createdb dbname
2. Verify that all users who own objects or were granted permissions on objects in the dumped
database already exist. If such users do not exist, the restore fails to recreate the objects with
the original ownership and permissions.
3. Run the psql utility to restore a text file dump created by the pg_dump utility:
where dumpfile is the output of the pg_dump command. To restore a non-text file dump, use
the pg_restore utility instead:
$ pg_restore non-plain-text-file
To restore data from a database cluster that you dumped using the pg_dumpall utility, follow the steps
below.
Prerequisites
You must run the commands as the postgres superuser or a user with database administrator
privileges.
Procedure
1. Ensure that all users who own objects or were granted permissions on objects in the dumped
databases already exist. If such users do not exist, the restore fails to recreate the objects with
the original ownership and permissions.
2. Run the psql utility to restore a text file dump created by the pg_dumpall utility:
59
Red Hat Enterprise Linux 9 Configuring and using database servers
Dumping a database directly from one server to another is possible because pg_dump and psql can
write to and read from pipes.
Procedure
By default, psql continues to execute if an SQL error occurs, causing the database to restore only
partially.
To change the default behavior, use one of the following approaches when restoring a dump.
Prerequisites
You must run the commands as the postgres superuser or a user with database administrator
privileges.
Procedure
Make psql exit with an exit status of 3 if an SQL error occurs by setting the ON_ERROR_STOP
variable:
Specify that the whole dump is restored as a single transaction so that the restore is either fully
completed or canceled.
$ psql -1
$ pg_restore -e
Note that when using this approach, even a minor error can cancel a restore operation that
has already run for many hours.
60
CHAPTER 4. USING POSTGRESQL
File system level backing up has the following advantage compared to other PostgreSQL backup
methods:
File system level backing up has the following limitations compared to other PostgreSQL backup
methods:
This backing up method is not suitable when you want to upgrade from RHEL 8 to RHEL 9 and
migrate your data to the upgraded system. File system level backup is specific to an architecture
and a RHEL major version. You can restore your data on your RHEL 8 system if the upgrade is
not successful but you cannot restore the data on a RHEL 9 system.
The database server must be shut down before backing up and restoring data.
Backing up and restoring certain individual files or tables is impossible. Backing up a file system
works only for complete backing up and restoring of an entire database cluster.
To perform file system level backing up, use the following procedure.
Procedure
# postgresql-setup --initdb
3. Use any method to create a file system backup, for example a tar archive:
Additional resources
61
Red Hat Enterprise Linux 9 Configuring and using database servers
PostgreSQL records every change made to the database’s data files into a write ahead log (WAL) file
that is available in the pg_wal/ subdirectory of the cluster’s data directory. This log is intended primarily
for a crash recovery. After a crash, the log entries made since the last checkpoint can be used for
restoring the database to a consistency.
The continuous archiving method, also known as an online backup, combines the WAL files with a copy
of the database cluster in the form of a base backup performed on a running server or a file system level
backup.
If a database recovery is needed, you can restore the database from the copy of the database cluster
and then replay log from the backed up WAL files to bring the system to the current state.
With the continuous archiving method, you must keep a continuous sequence of all archived WAL files
that extends at minimum back to the start time of your last base backup. Therefore the ideal frequency
of base backups depends on:
The maximum possible duration of data recovery in situations when recovery is necessary. In
cases with a long period since the last backup, the system replays more WAL segments, and the
recovery therefore takes more time.
NOTE
You cannot use pg_dump and pg_dumpall SQL dumps as a part of a continuous
archiving backup solution. SQL dumps produce logical backups and do not contain
enough information to be used by a WAL replay.
To perform a database backup and restore using the continuous archiving method, follow these
instructions:
1. Set up and test your procedure for archiving WAL files - see WAL archiving.
To restore your data, follow instructions in Restoring database with continuous archiving .
Continuous archiving has the following advantages compared to other PostgreSQL backup methods:
With the continuous backup method, it is possible to use a base backup that is not entirely
consistent because any internal inconsistency in the backup is corrected by the log replay.
Therefore you can perform a base backup on a running PostgreSQL server.
A file system snapshot is not needed; tar or a similar archiving utility is sufficient.
Continuous backup can be achieved by continuing to archive the WAL files because the
sequence of WAL files for the log replay can be indefinitely long. This is particularly valuable for
large databases.
Continuous backup supports point-in-time recovery. It is not necessary to replay the WAL
entries to the end. The replay can be stopped at any point and the database can be restored to
its state at any time since the base backup was taken.
If the series of WAL files are continuously available to another machine that has been loaded
62
CHAPTER 4. USING POSTGRESQL
If the series of WAL files are continuously available to another machine that has been loaded
with the same base backup file, it is possible to restore the other machine with a nearly-current
copy of the database at any point.
Continuous archiving has the following disadvantages compared to other PostgreSQL backup methods:
Continuous backup method supports only restoration of an entire database cluster, not a
subset.
A running PostgreSQL server produces a sequence of write ahead log (WAL) records. The server
physically divides this sequence into WAL segment files, which are given numeric names that reflect
their position in the WAL sequence. Without WAL archiving, the segment files are reused and renamed
to higher segment numbers.
When archiving WAL data, the contents of each segment file are captured and saved at a new location
before the segment file is reused. You have multiple options where to save the content, such as an NFS-
mounted directory on another machine, a tape drive, or a CD.
Procedure
c. Specify the shell command in the archive_command configuration parameter. You can use
the cp command, another command, or a shell script.
3. Test your archive command and ensure it does not overwrite an existing file and that it returns a
nonzero exit status if it fails.
4. To protect your data, ensure that the segment files are archived into a directory that does not
have group or world read access.
NOTE
63
Red Hat Enterprise Linux 9 Configuring and using database servers
NOTE
The archive command is executed only on completed WAL segments. A server that
generates little WAL traffic can have a substantial delay between the completion of a
transaction and its safe recording in archive storage. To limit how old unarchived data can
be, you can:
Set the archive_timeout parameter to force the server to switch to a new WAL
segment file with a given frequency.
This example shows a simple shell command you can set in the archive_command configuration
parameter.
The following command copies a completed segment file to the required location:
where the %p parameter is replaced by the relative path to the file to archive and the %f parameter
is replaced by the file name.
This command copies archivable WAL segments to the /mnt/server/archivedir/ directory. After
replacing the %p and %f parameters, the executed command looks as follows:
Additional resources
PostgreSQL 16 Documentation
You can create a base backup in several ways. The simplest way of performing a base backup is using the
pg_basebackup utility on a running PostgreSQL server.
The base backup process creates a backup history file that is stored into the WAL archive area and is
named after the first WAL segment file that you need for the base backup.
The backup history file is a small text file containing the starting and ending times, and WAL segments of
the backup. If you used the label string to identify the associated dump file, you can use the backup
history file to determine which dump file to restore.
NOTE
Consider keeping several backup sets to be certain that you can recover your data.
64
CHAPTER 4. USING POSTGRESQL
Prerequisites
You must run the commands as the postgres superuser, a user with database administrator
privileges, or another user with at least REPLICATION permissions.
You must keep all the WAL segment files generated during and after the base backup.
Procedure
If you use tablespaces and perform the base backup on the same host as the server, you
must also use the --tablespace-mapping option, otherwise the backup will fail upon an
attempt to write the backup to the same location.
To restore such data, you must manually extract the files in the correct locations.
2. After the base backup process is complete, safely archive the copy of the database cluster and
the WAL segment files used during the backup, which are specified in the backup history file.
3. Delete WAL segments numerically lower than the WAL segment files used in the base backup
because these are older than the base backup and no longer needed for a restore.
To specify which database server pg_basebackup will contact, use the following command-line options:
Additional resources
65
Red Hat Enterprise Linux 9 Configuring and using database servers
Procedure
If you do not have enough space, save the contents of the cluster’s pg_wal directory, which can
contain logs that were not archived before the system went down.
3. Remove all existing files and subdirectories under the cluster data directory and under the root
directories of any tablespaces you are using.
The files are restored with the correct ownership (the database system user, not root).
6. Copy any unarchived WAL segment files that you saved in step 2 into pg_wal/.
7. Create the recovery.conf recovery command file in the cluster data directory and specify the
shell command in the restore_command configuration parameter. You can use the cp
command, another command, or a shell script. For example:
The server will enter the recovery mode and proceed to read through the archived WAL files
that it needs.
If the recovery is terminated due to an external error, the server can be restarted and it will
continue the recovery. When the recovery process is completed, the server renames
recovery.conf to recovery.done. This prevents the server from accidental re-entering the
recovery mode after it starts normal database operations.
9. Check the contents of the database to verify that the database has recovered into the required
state.
If the database has not recovered into the required state, return to step 1. If the database has
recovered into the required state, allow the users to connect by restoring the client
authentication configuration in the pg_hba.conf file.
For more information about restoring using the continuous backup, see PostgreSQL Documentation.
66
CHAPTER 4. USING POSTGRESQL
On RHEL, you can use two PostgreSQL migration paths for the database files:
The fast upgrade method is quicker than the dump and restore process. However, in certain cases, the
fast upgrade does not work, and you can only use the dump and restore process, for example in case of
cross-architecture upgrades.
As a prerequisite for migration to a later version of PostgreSQL, back up all your PostgreSQL
databases.
Dumping the databases and performing backup of the SQL files is required for the dump and restore
process and recommended for the fast upgrade method.
Before migrating to a later version of PostgreSQL, see the upstream compatibility notes for the version
of PostgreSQL to which you want to migrate, and for all skipped PostgreSQL versions between the one
you are migrating from and the target version.
67
Red Hat Enterprise Linux 9 Configuring and using database servers
NOTE
Ensure that the mydbuser access is configured appropriately in the pg_hba.conf file.
See Creating PostgreSQL users for more information.
Since PostgreSQL 15, the libpq PQsendQuery() function is no longer supported in pipeline mode.
Modify affected applications to use the PQsendQueryParams() function instead.
The following procedure describes migration from the RHEL 8 version of PostgreSQL 12 to the RHEL 9
version of PostgreSQL 13 using the fast upgrade method. For migration from postgresql streams other
than 12, use one of the following approaches:
Update your PostgreSQL server to version 12 on RHEL 8 and then use the pg_upgrade utility
to perform the fast upgrade to RHEL 9 version of PostgreSQL 13.
Use the dump and restore upgrade directly between any RHEL 8 version of PostgreSQL and an
equal or later PostgreSQL version in RHEL 9.
Prerequisites
Before performing the upgrade, back up all your data stored in the PostgreSQL databases. By
default, all data is stored in the /var/lib/pgsql/data/ directory on both the RHEL 8 and RHEL 9
systems.
Procedure
68
CHAPTER 4. USING POSTGRESQL
Optionally, if you used any PostgreSQL server modules on RHEL 8, install them also on the
RHEL 9 system in two versions, compiled both against PostgreSQL 12 (installed as the
postgresql-upgrade package) and the target version of PostgreSQL 13 (installed as the
postgresql-server package). If you need to compile a third-party PostgreSQL server module,
build it both against the postgresql-devel and postgresql-upgrade-devel packages.
Basic configuration: On the RHEL 9 system, check whether your server uses the default
/var/lib/pgsql/data directory and the database is correctly initialized and enabled. In
addition, the data files must be stored in the same path as mentioned in the
/usr/lib/systemd/system/postgresql.service file.
PostgreSQL servers: Your system can run multiple PostgreSQL servers. Ensure that the
data directories for all these servers are handled independently.
PostgreSQL server modules: Ensure that the PostgreSQL server modules that you used
on RHEL 8 are installed on your RHEL 9 system as well. Note that plugins are installed in the
/usr/lib64/pgsql/ directory.
3. Ensure that the postgresql service is not running on either of the source and target systems at
the time of copying data.
4. Copy the database files from the source location to the /var/lib/pgsql/data/ directory on the
RHEL 9 system.
5. Perform the upgrade process by running the following command as the PostgreSQL user:
# postgresql-setup --upgrade
su postgres -c '~/analyze_new_cluster.sh'
69
Red Hat Enterprise Linux 9 Configuring and using database servers
NOTE
You may need to use ALTER COLLATION name REFRESH VERSION, see
the upstream documentation for details.
9. If you want the new PostgreSQL server to be automatically started on boot, run:
You can use this method for migrating data from any RHEL 8 version of PostgreSQL to any equal or
later version of PostgreSQL in RHEL 9.
On RHEL 8 and RHEL 9 systems, PostgreSQL data is stored in the /var/lib/pgsql/data/ directory by
default.
To perform the dump and restore upgrade, change the user to root.
The following procedure describes migration from the RHEL 8 default version of PostgreSQL 10 to the
RHEL 9 version of PostgreSQL 13.
Procedure
2. On the RHEL 8 system, dump all databases contents into the pgdump_file.sql file:
Optionally, if you used any PostgreSQL server modules on RHEL 8, install them also on the
RHEL 9 system. If you need to compile a third-party PostgreSQL server module, build it against
the postgresql-devel package.
70
CHAPTER 4. USING POSTGRESQL
5. On the RHEL 9 system, initialize the data directory for the new PostgreSQL server:
# postgresql-setup --initdb
6. On the RHEL 9 system, copy the pgdump_file.sql into the PostgreSQL home directory, and
check that the file was copied correctly:
/var/lib/pgsql/data/pg_hba.conf
/var/lib/pgsql/data/pg_ident.conf
/var/lib/pgsql/data/postgresql.conf
9. On the RHEL 9 system, import data from the dumped sql file:
71