Upgrade the embedded database to PostgreSQL 18

From release 6.4.4, Email Gateway Security uses PostgreSQL 18 for the embedded database (see Standalone deployment). A new installation creates the embedded database on PostgreSQL 18. An upgrade from an earlier release keeps the PostgreSQL 14 database, and Email Gateway Security continues to operate on it. Administrators should convert this database to PostgreSQL 18 in a maintenance window. For the supported PostgreSQL versions, see Prerequisites.

This page describes the conversion with the Upgrade-EmbeddedDatabase.ps1 script.

External database

This page is only for the embedded database. For an external database server, see Upgrade an external database to PostgreSQL 18. The script refuses to run on a system with an external database.

Overview

Who needs the conversion

The conversion is for systems with the embedded database that were upgraded to release 6.4.4 or later from an earlier release. These signs show that the database still runs on PostgreSQL 14:

  • The management console shows the Database upgrade required notification. You cannot dismiss it


  • The About dialog shows a 14.x release in the PostgreSQL Version row. To open the dialog, click the user icon in the upper right corner of the management console, then click About product.


  • During the upgrade of Email Gateway Security, the installer shows a notification on the Ready to Install dialog. For details see Installation


What the script does

The script converts the database in place with pg_upgrade in link mode (--link). The port, the data and the schema version stay the same. During the conversion, Email Gateway Security does not process emails, and the management console is not available.

The script runs these steps:

Step

What happens

1

The script checks the preconditions (see Troubleshooting below)

2

It asks for the password of the database superuser and tests it

3

It reads the locale, the schema version and the size of the database

4

It asks about the checksum step and the backup. Then it shows a summary of the downtime and asks for a confirmation

5

It stops Email Gateway Security

6

It makes a backup with pg_dumpall (the roles) and pg_dump (the database). This step is optional

7

It stops the database service

8

It enables data checksums on the PostgreSQL 14 database with pg_checksums. This step is optional

9

It reads the checksum setting of the PostgreSQL 14 database

10

It creates the folder of the new database

11

It creates the PostgreSQL 18 database cluster with initdb

12

It runs pg_upgrade --check. This test changes nothing

13

It runs pg_upgrade --link

14

It handles the custom settings in postgresql.conf, pg_hba.conf and postgresql.auto.conf (see Custom PostgreSQL settings below)

15

It asks before the first start of PostgreSQL 18. This is the point of no return

16

It renames the database folders

17

It starts PostgreSQL 18 and checks it

18

It updates the database statistics with vacuumdb and starts Email Gateway Security. When necessary, it also rebuilds the search indexes with REINDEX

19

It shows the result

From step 5 until the first start of PostgreSQL 18, the Email Gateway Security services and the database service have the start type Disabled. So a restart of the server in this time does not start a half-converted database.

Link mode: fast, but no way back after the first start

pg_upgrade can copy the data files into the new database cluster (copy mode, the default of pg_upgrade), or it can make hard links to the existing data files (link mode). The script uses link mode:

  • Advantage: link mode does not copy the data, so it is much faster and needs almost no extra disk space. In the tests of OPSWAT, this step took about one minute at every database size

  • Risk: after link mode, the old and the new database cluster use the same data files. As soon as PostgreSQL 18 starts, the old PostgreSQL 14 cluster cannot be used any more

  • How the script handles the risk: until the first start of PostgreSQL 18, the script can go back to PostgreSQL 14 with the data unchanged. It asks before the first start (step 15). After the first start, only a backup or a system snapshot brings back PostgreSQL 14

In copy mode, the old cluster would stay usable, but the conversion would take much longer, and the volume would need free space for a second copy of the database. For details see pg_upgrade.

Before you start

  1. Upgrade Email Gateway Security to release 6.4.4 or later. For details see Installation

  2. In a scalable deployment, run the script on the instance that holds the shared embedded database. On the other instances, the script refuses to run, because they use the database as an external database. The script stops only the services on its own computer. We recommend that you also stop the Email Gateway Security services on the other instances during the conversion. If you do not stop them, they cannot use the database during the conversion. They may work again after the conversion, but OPSWAT cannot guarantee it. For details see Scalable deployment operation

  3. Plan a maintenance window. In step 4, the script shows the expected downtime. For rough figures, see Data checksums below

  4. Make a system snapshot of the server, for example a VM snapshot or a full image backup. The script can also make a backup with pg_dump. Without a backup, the script asks you to confirm that a current system snapshot exists

  5. Make sure that the volume of the database folder has at least 5 GiB of free space. A backup on the same volume needs more space, and the script warns you about it

  6. Make sure that the paths of the installation folder, the data folder and the backup folder have only ASCII characters

  7. Have the password of the database superuser ready. The script asks for it and does not store it. If you do not know the password, contact OPSWAT support before the maintenance window

  8. Use a PowerShell window that runs as administrator

Check the script signature

The script is in the tools folder of the installation folder (see Directories). By default, this is C:\Program Files\OPSWAT\Metadefender Email Security\tools\Upgrade-EmbeddedDatabase.ps1. OPSWAT signs the script. Check the signature before you run it:

Get-AuthenticodeSignature "C:\Program Files\OPSWAT\Metadefender Email Security\tools\Upgrade-EmbeddedDatabase.ps1"

The Status must be Valid, and the signer must be OPSWAT, Inc. If the status is HashMismatch, the file was changed after OPSWAT signed it. Do not run it, and contact OPSWAT support.

If the execution policy of the computer is AllSigned, PowerShell asks at the first run if you trust the publisher. Answer Run once or Always run. A scheduled task that uses the AllSigned policy can run the script only after the publisher is trusted.

Run the conversion

  1. Open PowerShell as administrator

  2. Start the script:

cd "C:\Program Files\OPSWAT\Metadefender Email Security\tools"
.\Upgrade-EmbeddedDatabase.ps1
  1. Answer the questions of the script. If you press Enter, the script uses the default answer:

Question

Default

Enable data checksums before the conversion?

No

Make a backup with pg_dump before the conversion?

Yes. The script then asks for the backup folder. The folder must not exist, but its parent folder must exist

No backup is selected. Does a current system snapshot of this server (a VM snapshot or a full image backup) exist?

No. Without a backup or a snapshot, the script stops and changes nothing

Stop EGS and start the conversion now?

No

Copy the valid custom settings to the new postgresql.conf?

Yes

Start PostgreSQL 18 now? This is the point of no return.

No

Start EGS now?

Yes

Delete the old cluster folder?

No

Do not close the window

Do not close the PowerShell window while the script runs. To stop the script, press Ctrl+C. A closed window leaves Email Gateway Security and the database service stopped until you run the script again or run it with -Revert.

Parameters

Parameter

Description

-Credential

The password of the database superuser as a credential object. The user name is not used

-NonInteractive

The script asks no questions. It needs -BackupFolder or -SnapshotConfirmed. It starts PostgreSQL 18 and Email Gateway Security without a question, and it never restores a backup

-BackupFolder

Make a backup with pg_dump into this folder before the database service stops. The folder must not exist, but its parent folder must exist

-NoBackup

Do not ask for a backup. The script then asks you to confirm the system snapshot

-SnapshotConfirmed

Confirms that a current system snapshot of this server exists

-EnableChecksums

Enable data checksums, then convert the database

-EnableChecksumsOnly

Enable data checksums, then start Email Gateway Security again on PostgreSQL 14. The script does not convert the database

-Revert

Go back to the last working state of an unfinished earlier run: PostgreSQL 14, with Email Gateway Security started. This works until the first start of PostgreSQL 18

-Postgre14Path, -Postgre18Path

The folders of the PostgreSQL 14 and PostgreSQL 18 programs. The defaults are the postgre14 and postgre folders in the installation folder

Example of a run without questions, with a backup:

$credential = Get-Credential -UserName postgres -Message "Password of the database superuser"
.\Upgrade-EmbeddedDatabase.ps1 -NonInteractive -Credential $credential -BackupFolder "D:\EGS-backup"

Data checksums (optional)

New installations of release 6.4.4 have data checksums on. With data checksums, PostgreSQL can find damaged data pages. A converted database keeps the setting of the PostgreSQL 14 database, unless you enable checksums with the script.

  • Time: in the tests of OPSWAT, the checksum step took about 7 to 8 minutes per 100 GB on a fast local SSD, and about 14 minutes per 100 GB on a virtual machine, plus 1 to 2 minutes. The pg_upgrade --link step took about 1 minute at every tested size. Before Email Gateway Security stops, the script shows an estimate for the whole run

  • One or two maintenance windows: -EnableChecksums enables checksums and then converts the database. -EnableChecksumsOnly enables checksums now, and you convert the database in a later maintenance window. Two windows do not make the total downtime shorter

  • Stop: you can stop the checksum step at any time with Ctrl+C. No data is lost, but the next run starts the step again from 0 %

  • The checksum step does not check the existing data

Cancel, revert and restore

  • Until the first start of PostgreSQL 18 (step 17): Ctrl+C, a failure or the answer No brings back PostgreSQL 14 with the data unchanged, and Email Gateway Security starts again

  • After a closed window, a stopped process, a power loss or a restart of the server: run the script again. It offers the revert. With -Revert, the script does the revert without a question

  • After the first start of PostgreSQL 18: the only way back is a restore. If a later step fails, the script offers a restore with pg_restore to a new PostgreSQL 14 or PostgreSQL 18 database, but only in interactive mode and only with a backup of this run. The damaged database stays in the pg_data.failed folder. Without a backup, restore the system snapshot

The exit code of the script is 0 when the run is complete, when nothing changed after the answer No, or after a successful revert or restore. It is 1 when the script refused, failed or was stopped. After Ctrl+C or a closed window, the exit code is not reliable. Then read the Result field of the upgrade-state.json file (see After the conversion below).

After the conversion

  • The Database upgrade required notification disappears after Email Gateway Security starts on PostgreSQL 18. The About product dialog shows the PostgreSQL 18 minor release, for example 18.7


  • In a scalable deployment, if you stopped the Email Gateway Security services on the other instances, start them again. Check that all instances connect to the database. The script keeps the port, and it copies listen_addresses and the rules of pg_hba.conf (see Custom PostgreSQL settings below). For the settings that remote instances need, see Scalable deployment

  • When Email Gateway Security works well, delete these folders:

    • pg_data14.old: the old PostgreSQL 14 database, next to the pg_data folder. The script offers to delete it at the end of the run

    • The backup folder. It holds email data, so keep it only as long as you need it

    • pg_data.failed, next to the pg_data folder, if a restore made it

  • Logs: the script writes the log <data folder>\logs\pg-upgrade-<time>.log, and the folder <data folder>\pg-upgrade-<time> with the file upgrade-state.json. By default, the data folder is <installation folder>\data

Custom PostgreSQL settings

  • postgresql.conf: the script copies the custom values that are valid in PostgreSQL 18, for example listen_addresses, and it lists the others. It does not copy the port, because the new database uses the port from the Registry. Check the list after the conversion

  • pg_hba.conf: the script copies the file if it is different from the default file. It warns about trust rules. If PostgreSQL 18 does not accept the copied file, the script uses the default file and writes a warning. Then remote instances cannot connect until you add their rules again

  • postgresql.auto.conf: the script shows the values and writes them to the log, but it does not copy them

  • The Registry value postgre_folder: if the value is set, the script sets it to the PostgreSQL 18 folder. For details see Registry configuration

Troubleshooting

In these cases, the script refuses to start and changes nothing:

Reason

What to do

PowerShell does not run as administrator

Open PowerShell with Run as administrator

Another run of the script is active on the computer

Wait until the other run ends

A path has a character that is not ASCII

Use folders with ASCII characters only

The system uses an external database

See Upgrade an external database to PostgreSQL 18

The database already runs on PostgreSQL 18

Nothing to do

The folder pg_data14.old already exists

It is from an earlier attempt. Move it or delete it, then run the script again

The volume of the database folder has less than 5 GiB of free space

Free up disk space

The database does not ask for SCRAM (scram-sha-256) authentication

Change the settings that the message names, then run the script again

The password test failed

Check the password of the database superuser

-NonInteractive without -BackupFolder or -SnapshotConfirmed

Add one of the two parameters