In the last post, the first in this series about installing PostgreSQL, we looked at how to install PostgreSQL on Linux. This post is an alternative install method where we’ll look at how to install PostgreSQL on a Docker container, where the container is run on a Linux host. As before, we’ll run through the steps from install to logging in from a remote client.
Installing PostgreSQL on Docker differs from installing directly on the Linux OS, we’ll see how now.
The VM
I’ll use the same spec VM as in the last post, which is sufficient for demonstration purposes:
- 4-Core CPU
- 16GB RAM
- Debian 13
Install Docker
Let’s install Docker. I’ll follow the installation instructions on the Official Docker documentation. The commands below are copied verbatim from the instructions. I’ll SSH into a remote shell to run the commands.
Firstly, add the Docker apt repository and update our apt sources:
# Add Docker's official GPG key:
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to apt sources:
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/debian
Suites: $(. /etc/os-release && echo "$VERSION_CODENAME")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF
sudo apt update

Now install the various Docker packages:
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

And just like that, we now have Docker installed.
Install PostgreSQL
Now, with Docker installed, download the PostgreSQL Docker image. This command will pull the official Docker image from Docker Hub. I think of an image as a recipe to set up a specific system which in this case is PostgreSQL – it contains the binaries, the config locations, directories etc. By default, we get the latest version, though this can be altered as desired. The tags page shows the versions available to us.
sudo docker image pull postgres
The screenshot below shows the image pull in action, pulling its various dependencies:

The screenshot below shows the image has downloaded:

With the image downloaded, we can create and start the container. This is an instance of the image – a container that has been created using the recipe in the image:
sudo docker run --name PostgresContainer -p 5432:5432 -e POSTGRES_PASSWORD=MyPassword -d postgres
There are a couple of switches I’ll quickly explain:
-p 5432:5432
This exposes the container’s port 5432 (the PostgreSQL port) as 5432 on the host machine. When we connect to this instance, we will connect to the host machine’s IP or hostname on port 5432 and that passes through to 5432 in the container. This means we can have multiple PostgreSQL containers, each with their own internal port 5432 mapped to different host ports.
-e POSTGRES_PASSWORD=MyPassword -e POSTGRES_USER=postgres
-e relates to passing arguments to environment variables in the form VARIABLE_NAME=Value. We can see in the documentation for the image what environment variables are supported. In my case, I specified a password for the postgres user which is actually a mandatory parameter:
This environment variable is required for you to use the PostgreSQL image. It must not be empty or undefined.
-d
This runs the container detached, i.e. run in the background – we get control of our terminal back.
The final argument in the command (postgres) is the name of the image we are using to create the container.
After running the command, we get back a container ID:
No issues — the StackOverflow demo database isn’t used in this post, there’s no T-SQL to check (the code blocks are single-line PowerShell/pg_hba syntax, so no indentation problems are possible), and the PostgreSQL/Windows platform context is clear throughout.
That is our instance up and running.
Logging In Remotely
First, I’ll see if I can log in “Remotely” from the shell of the VM I have installed Docker on. As the PostgreSQL installation is on a container, to connect, we have to act like it is a remote machine as that is how it presents itself. We can install the PostgreSQL client tools with the following command:
sudo apt install postgresql-client postgresql-client-common
Once installed, we can then connect as we would to any remote instance:
psql -h localhost -p 5432 -U postgres

With that, we are connected.
Remote Connection Configuration – Not Required
In the previous post where we installed directly on the Linux VM, we had to change the configuration to allow remote logins. In the PostgreSQL Docker image, this configuration is already done for us. We can use docker exec to execute commands in the container shell to show us that the configuration files already have the relevant values (note the directory of the config files is different to the vanilla Debian install):
sudo docker exec PostgresContainer cat /var/lib/postgresql/18/docker/postgresql.conf | grep "listen"

We can see the listen_addresses config already exists. Furthermore, if we cat the pg_hba.conf file, we can see a rule has been added to allow all remote hosts:
sudo docker exec PostgresContainer cat /var/lib/postgresql/18/docker/pg_hba.conf

With that, we should be able to remotely connect with pgAdmin as we did in the last post. In this case I am connecting from a true remote machine, i.e. not the container host. We register a server within pgAdmin:

Once we click save, we see we are connected and we can see the databases:

With that, we have installed and remotely logged in to a PostgreSQL Docker container hosted on a Linux VM.
References / Further Reading
Docker, Inc – Install Docker Engine on Debian
DualCoreDBA – Four Flavours of Postgres Install – Part 1
The PostgreSQL Docker Community – postgres Official Docker Image
