How to Encrypt a Database at Rest in PostgreSQL on Ubuntu 22.04

10 ott 2023 1 min di lettura
How to Encrypt a Database at Rest in PostgreSQL on Ubuntu 22.04
Indice dei contenuti

Introduction

Encryption at Rest is an essential practice to ensure the security of sensitive data. PostgreSQL on Ubuntu 22.04 offers several options for encrypting your database at rest. In this tutorial, we will explore how to encrypt a database in PostgreSQL on Ubuntu 22.04.

Initial configuration

Installing PostgreSQL:

Make sure you have PostgreSQL installed on your Ubuntu 22.04 system. If you haven't already done so, you can install it with the following command:

sudo apt update
sudo apt install postgresql postgresql-contrib

Encryption Options

PostgreSQL supports several filesystem-level or block-level encryption options 1 ​. Filesystem-level encryption options include eCryptfs and EncFS, while for block-level or full disk encryption, options include dm-crypt + LUKS on Linux.

Filesystem Level Encryption with eCryptfs:

Install eCryptfs on your Ubuntu system with the following command:

sudo apt-get install ecryptfs-utils

Encryption Setup:

Load the eCryptfs kernel module with the following command:

sudo modprobe ecryptfs

Creating an Encrypted Partition:

Create an encrypted partition for your PostgreSQL database. Assuming the partition is /dev/sdaX, the command is:

sudo ecryptfs-setup-private --dev=/dev/sdaX

Mounting the Encrypted Partition:

Mount the encrypted partition to the desired mount point, for example /var/lib/postgresql:

sudo mount -t ecryptfs /dev/sdaX /var/lib/postgresql

PostgreSQL Configuration:

Edit the PostgreSQL configuration file ( postgresql.conf ) to indicate the new location of the encrypted database:

sudo nano /etc/postgresql/XX/main/postgresql.conf

Edit the data_directory line with the new path:

data_directory = '/var/lib/postgresql'

Conclusions

You have now successfully configured encryption of your PostgreSQL database at rest on Ubuntu 22.04. This will guarantee you greater security for your sensitive data, protecting them from unauthorized access. Be sure to test your setup in a secure environment before applying it in a production environment.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.