PostgreSQL is one of the most popular open-source relational databases, trusted for its stability, performance, and reliability. Whether you’re building web apps, APIs, or data-driven platforms, PostgreSQL is often the go-to choice for developers.
In this tutorial, we'll go through how to deploy a PostgreSQL docker container on a VPS. For this, we'll be using Serversinc, a server management platform that makes deploying apps and services a breeze.
You’ll also learn two recommended methods for connecting to PostgreSQL on Serversinc:
- Connecting an application on the same server directly to Postgres
- Accessing Postgres from your local machine or another remote server using an SSH tunnel
Step 1: Create a new application for Postgres
To begin, you’ll create a new application inside the Serversinc dashboard that will run the official PostgreSQL Docker image.
- In the Serversinc dashboard, navigate to My Applications.
- Click Create new application.
- Select the server where you’d like to deploy PostgreSQL from the dropdown
- Give the application a name, for example: Postgres.
- Leave the Type as Docker Image.
- Port: You don’t need to specify a port unless you plan on exposing PostgreSQL externally (which isn’t recommended for most setups). For internal usage, leave this blank.
- Leave Domain blank.
- In Image & Tag, enter
postgres:latest
(You can also pin to a specific version, such aspostgres:15
, if you want consistency across deployments.)
- Click Create application.
After creating the app, you’ll be redirected to the Application overview page. This page is your central management hub for the Postgres container, where you can:
- View deployment history
- Check environment variables
- Manage attached volumes
- Inspect the currently running container
This is where you’ll return any time you want to update, redeploy, or check the status of your PostgreSQL instance.
Step 2: Configure environment variables
PostgreSQL relies on a few environment variables when the container starts. These tell it which user to create, what password to assign, and what the default database should be.
- Open the Environment tab of your new Postgres application.
- Add the required environment variables. For example:
POSTGRES_USER=myuser
POSTGRES_PASSWORD=securepassword
POSTGRES_DB=mydb
These values will:
- Create a new database user (
POSTGRES_USER
) - Assign it a password (
POSTGRES_PASSWORD
) - Create a default database (
POSTGRES_DB
) owned by that user
You can add additional environment variables if needed (such as locale or encoding options), but for most cases, these three are enough to get started.
Step 3: Add a volume for persistence
By default, Docker containers are ephemeral, meaning all data is stored inside the container. If you redeploy, update, or remove the container, the database would be lost. To prevent this, you should mount a volume so that PostgreSQL writes data to the server’s disk.
- Go to the Volumes tab of your Postgres application.
- Create a new volume and mount it to Postgres’s data directory:
postgres-data:/var/lib/postgresql/data
- Save the volume configuration
Ensure the postgres-data directory exists too, you can run commands on your server from the Dashboard to create it beforehand.
Now your database files will persist across redeployments and container restarts, ensuring your data stays safe.
Step 4: Deploy PostgreSQL
With everything configured, you’re ready to launch your PostgreSQL container.
- Open the Deployments tab in your Postgres application.
- Click Deploy.
Serversinc will now:
- Pull the official PostgreSQL Docker image
- Apply your environment variables (user, password, database)
- Mount the persistent volume for data storage
- Start the container on your selected server
Within a few moments, PostgreSQL will be up and running, ready for connections. You can confirm the deployment status and logs directly from the Application overview tab.
That’s it — you now have a fully functioning PostgreSQL database running on your server, managed through Serversinc.
Step 5: Connecting to Postgres
Once your PostgreSQL container is running, you can connect to it in a few ways depending on your setup.
Option 1: Connect from an application on the same server
If your app is running on the same server as PostgreSQL, you can connect directly using the container’s internal network:
- Host: localhost or the container’s internal hostname
- Port: 5432 (default PostgreSQL port)
- Username: The value you set in
POSTGRES_USER
- Password: The value you set in
POSTGRES_PASSWORD
- Database: The value you set in
POSTGRES_DB
For example, in Node.js using pg:
const { Pool } = require('pg');
const pool = new Pool({
host: 'localhost',
port: 5432,
user: 'myuser',
password: 'securepassword',
database: 'mydb',
});
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res);
pool.end();
});
This method keeps your database private and avoids exposing PostgreSQL to the internet.
Option 2: Connect via SSH tunnel
If you need to connect from your local machine or another remote server, an SSH tunnel lets you securely access PostgreSQL without opening ports publicly:
ssh -L 5432:localhost:5432 user@your-server-ip
-
user@your-server-ip
→ Replace with your server login -
5432:localhost:5432
→ Maps your local port to the server’s PostgreSQL port
Once the tunnel is active, you can connect using localhost:5432
as if you were on the server itself.
This method ensures that your database remains secure behind your server’s firewall while still allowing remote access.
Not already using SSH Keys? Read our guide on how to Generate SSH Keys and Connect to Your VPS Securely
Benefits of running PostgreSQL on Serversinc
Running PostgreSQL on Serversinc combines the power of a self-hosted database with simple management and monitoring:
- Easy Deployment: Launch a Postgres container in just a few clicks, with environment variables and persistent storage pre-configured.
- Full Control: Retain complete control over your server, database versions, and configuration.
- Integrated Monitoring: Check logs, view deployment history, and receive alerts if your Postgres container stops — all from the dashboard.
Wrapping up
Deploying PostgreSQL on Serversinc is fast, secure, and fully under your control. In just a few clicks, you can launch a containerized Postgres instance, configure environment variables, attach a persistent volume, and connect your applications on the same server or remotely via SSH.
With Serversinc’s dashboard, you can monitor your database, view logs, and receive alerts if anything goes wrong, giving you peace of mind and full visibility.
For small projects up to production-grade applications, running PostgreSQL on Serversinc makes database management simple, reliable, and efficient.
Top comments (0)