How to Run PostgreSQL in Docker on a Plesk Server

Learn how to run PostgreSQL 17 in a Docker container on your Plesk server with persistent storage, secure connections, and simple backups. This step-by-step guide covers both the Plesk UI and Docker CLI so you can choose the workflow that fits your needs.

Table of Contents

Tested on: Plesk Obsidian (Linux), Ubuntu 22.04 host

Running PostgreSQL in Docker on a Plesk box is a clean way to keep your database isolated, version-pinned, and easy to back up and upgrade—without fighting the host’s package manager or Plesk’s PHP stacks. This guide walks you through a secure, persistent, and maintainable setup using either the Plesk UI or the Docker CLI.


What You’ll Build

By the end of this guide, you will have:

  • A PostgreSQL 17 container running on your server
  • Persistent storage that ensures your data survives container restarts or upgrades
  • Secure network exposure so only trusted applications or hosts can connect
  • A clear process for backups, restores, and upgrades
  • Optional tools such as pgAdmin for browser-based management

Prerequisites

Before getting started, make sure you have the following in place:

  • Plesk Obsidian on a Linux server with root/SSH access
  • The Plesk Docker extension installed (Plesk → Extensions → “Docker”)
  • Shell access to run docker commands (root or a user in the docker group)
  • Control over the server’s firewall, whether that’s Cloudflare, AWS Security Groups, or a local firewall such as ufw

Note: This guide was tested on Ubuntu 22.04, which does not use SELinux by default. If you are running AlmaLinux or Rocky Linux with SELinux enabled, you may need to adjust security contexts (:Z or :z flag with Docker CLI) so that the container can read and write to the host directory.


Option A — Using the Plesk UI (Docker Extension)

If you prefer to manage containers through the Plesk control panel, the Docker extension allows you to configure PostgreSQL without touching the command line. This is especially useful if you are already using Plesk for hosting and want to keep everything in one place.

Step 1: Open the Docker Extension

Log in to Plesk and go to the Docker section. If you do not see it, install the extension from Extensions → Catalog. Once installed, open Docker and click + Add Container.

Step 2: Choose the PostgreSQL Image

Search for postgres:17 in the image field. Select the official PostgreSQL 17 image from Docker Hub and click Run.

Step 3: Configure Environment Variables

Set the required environment variables for PostgreSQL. At a minimum, you must provide a superuser password:

  • POSTGRES_PASSWORD = strong-password-here

Optionally, you can create a user and a database at startup:

  • POSTGRES_USER = appuser
  • POSTGRES_DB = appdb

If you skip these, PostgreSQL will create only the default postgres user.

Step 4: Configure Volumes

In Plesk, the Host and Container fields define the bind mount path. The container path must always be /var/lib/postgresql/data, since that is where PostgreSQL stores its database files. The host path must be an absolute directory on your server, such as /srv/pg17/data.

Example:

  • Host: /srv/pg17/data
  • Container: /var/lib/postgresql/data

Before creating the container, prepare this directory on your server:

Callout:
On Ubuntu 22.04 (the tested environment), creating the directory and setting ownership is enough.
On AlmaLinux or Rocky Linux, if SELinux is enabled, you may also need to add a :Z flag in Docker CLI runs or adjust the directory’s SELinux context so PostgreSQL can write to it. This step is not necessary on Ubuntu.

Step 5: Configure Ports

If the database is only used by applications on the same server, bind it to localhost only. In Plesk, set Published port to 127.0.0.1:5432 and Container port to 5432.

If you need remote access, you may expose the port to all interfaces (0.0.0.0:5432), but you should combine this with firewall rules so only trusted IP addresses can connect. Never expose PostgreSQL directly to the internet without restrictions.

Step 6: Restart Policy

Select Always as the restart policy so PostgreSQL will automatically come back online if the container crashes or the server reboots.

Step 7: Launch and Verify

Click Run. Once the container is running, test the connection from your server:

If you can log in and run queries, PostgreSQL is successfully running under Docker in Plesk.


Option B — Using the Docker CLI

For administrators who prefer the terminal, Docker CLI provides full control and flexibility. With a few commands, you can start PostgreSQL, configure persistence, and manage upgrades.

Using a Named Volume

A named volume is the simplest way to manage persistence. Docker will handle where the files are stored on the host.

Using a Bind Mount

If you prefer to know exactly where the files live on your host, you can bind a specific directory.

Verifying the Setup

Check the logs to make sure PostgreSQL started correctly:

Run a quick test query:


Option — Creating a New Database and User

Often, you will need to create new databases and credentials for your applications. This can be done inside the container once PostgreSQL is running.

  1. Open a shell inside the container:
  2. Connect as the superuser:
  3. Create a new database:
  4. Create a new user and password:
  5. Grant privileges to the user:
  6. Test the new login:

This ensures your applications use dedicated accounts instead of the postgres superuser, which improves security.


Connecting Your Plesk-Hosted App

Applications running on the same server should connect to PostgreSQL at localhost:5432. Remote access is possible but should always be restricted with firewalls or VPNs.

Example .env connection string:


Backups and Restores

To keep your data safe, you should back up regularly and test your restore process.

Creating a backup:

Restoring a backup:

Automating these steps with cron and copying backups off the server (e.g., to S3 or another host) is highly recommended.


Upgrading PostgreSQL

When upgrading within the same major version (e.g., 17.1 → 17.2), you can stop the container, pull the latest image, and restart with the same volume.

For major version upgrades (e.g., 17 → 18), the safest method is to use pg_dump from the old container and pg_restore into a new one. This avoids compatibility issues with data formats.


Logs and Monitoring

You can check logs with:

For advanced monitoring, consider using postgres_exporter with Prometheus and Grafana. This gives you insights into query performance, replication status, and resource usage.


Hardening Checklist

  • Always use strong, unique credentials.
  • Bind PostgreSQL to localhost unless remote access is required.
  • Restrict public access with firewalls or VPNs.
  • Perform daily backups and test restores.
  • Run applications with dedicated, least-privilege database users.
  • Keep Docker images updated with security patches.

Optional: Running pgAdmin

For a web-based GUI, you can run pgAdmin alongside PostgreSQL. Limit access to localhost and tunnel securely if you need browser access.


Need Help?

Reliable Penguin focuses on systems administration and managed hosting—including secure Dockerized databases on Plesk, backups, monitoring, and upgrades. If you want a rock-solid setup without babysitting servers, we can help.

 

Have a project or a problem?

Talk with a senior engineer for practical recommendations—no obligation.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Categories

Get a free consultation from Reliable Penguin

Submit the form—or for immediate service call 866-649-7984.