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
dockercommands (root or a user in thedockergroup) - 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 (
:Zor:zflag 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 = appuserPOSTGRES_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:
|
1 2 3 |
mkdir -p /srv/pg17/data chown -R 999:999 /srv/pg17/data |
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:Zflag 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:
|
1 2 |
psql -h 127.0.0.1 -U appuser -d appdb -p 5432 |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
docker volume create pg17-data docker run -d \ --name pg17 \ --restart=always \ -e POSTGRES_PASSWORD='strong-password-here' \ -e POSTGRES_USER='appuser' \ -e POSTGRES_DB='appdb' \ -v pg17-data:/var/lib/postgresql/data \ -p 127.0.0.1:5432:5432 \ postgres:17 |
Using a Bind Mount
If you prefer to know exactly where the files live on your host, you can bind a specific directory.
|
1 2 3 4 5 6 7 8 9 10 |
mkdir -p /srv/pg17/data docker run -d \ --name pg17 \ --restart=always \ -e POSTGRES_PASSWORD='strong-password-here' \ -v /srv/pg17/data:/var/lib/postgresql/data:Z \ -p 127.0.0.1:5432:5432 \ postgres:17 |
Verifying the Setup
Check the logs to make sure PostgreSQL started correctly:
|
1 2 |
docker logs -f pg17 |
Run a quick test query:
|
1 2 |
docker exec -it pg17 psql -U postgres -c "SELECT version();" |
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.
- Open a shell inside the container:
12docker exec -it pg17 bash - Connect as the superuser:
12psql -U postgres - Create a new database:
12CREATE DATABASE myappdb; - Create a new user and password:
12CREATE USER myappuser WITH PASSWORD 'strongpasswordhere'; - Grant privileges to the user:
12GRANT ALL PRIVILEGES ON DATABASE myappdb TO myappuser; - Test the new login:
12psql -h 127.0.0.1 -U myappuser -d myappdb
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:
|
1 2 |
DATABASE_URL=postgres://appuser:strong-password-here@127.0.0.1:5432/appdb |
Backups and Restores
To keep your data safe, you should back up regularly and test your restore process.
Creating a backup:
|
1 2 3 |
docker exec pg17 pg_dump -U appuser -d appdb -Fc -f /tmp/appdb_$(date +%F).dump docker cp pg17:/tmp/appdb_2025-09-11.dump /srv/backups/ |
Restoring a backup:
|
1 2 3 4 |
docker exec -it pg17 createdb -U appuser appdb docker cp /srv/backups/appdb_2025-09-11.dump pg17:/tmp/ docker exec -it pg17 pg_restore -U appuser -d appdb /tmp/appdb_2025-09-11.dump |
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:
|
1 2 |
docker logs pg17 |
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.
|
1 2 3 4 5 6 7 8 |
docker run -d \ --name pgadmin \ --restart=always \ -e PGADMIN_DEFAULT_EMAIL='admin@example.com' \ -e PGADMIN_DEFAULT_PASSWORD='another-strong-password' \ -p 127.0.0.1:8081:80 \ dpage/pgadmin4 |
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.




