Creating a postgres container using podman with Extensions
In my previous post i outlined how to create a container with the latest postgresql version (At the time of writing postgres 18) running with a few extensions installed. podman is a daemonless, open source, linux native tool designed to make it easy to find, run, build, share and deploy applications using Open Containers Initiative (OCI) Containers. Using podman avoids the additional overhead of needing to have the docker deamon running and is simpler to use.
Postgresql Official images for Docker
The steps outlined below, composes a new local container image from the latest postgres image available in the docker hub, and runs a container based on the new image.
Install the podman binary for your linux platform.
First I create a directory to hold my podman compose files. All of the following steps are run as the non-root user we installed podman with.
mkdir ~/Apps/docker/pg/pg1
cd ~/Apps/docker/pg/pg1
Create a file Named Containerfile and add the following lines to it. Here we are specifying that podman use the image tagged postgres:latest (This will be the latest generally available version of postgres which is 18 at the time of writing), and install the packages postgresql-contrib (Gets you all the extensions that are contrib modules) and pgvector.
FROM docker.io/library/postgres:latest
# Update package lists
RUN apt-get update
# Install PostgreSQL extensions
RUN apt-get install -y --no-install-recommends \
postgresql-contrib \
postgresql-18-pgvector
# Clean up
RUN rm -rf /var/lib/apt/lists/*
Run the following command to build the new image from the base image postgres:latest. This simply builds a new image and does not create a container or start it.
podman build .
Run the following command to list the image we just created and note down the imageid (We will use it in the next step to run the container using this image). You will see 4 images being listed, pick the image that is the latest (use the created column to determine the latest image) (podman creates some intermediate images, as it is building the image by adding the extensions).
podman image ls -a
Run the following command to run a container based on the image we just built. Replace the username, password and database name in the command below with your choices. Replace 27e3aca52506 with the image id we gathered up from the previous step.
podman run -d -p 5432:5432 --name pg1local -e POSTGRES_PASSWORD=pg1pwd -e POSTGRES_USER=pg1user -e POSTGRES_DB=pg1db \
-v /home/rkadmin/Apps/docker/pg/pg2/postgresql.conf:/etc/postgresql/postgresql.conf 27e3aca52506 -c 'config_file=/etc/postgresql/postgresql.conf'
In the above command you see that I am using the -v switch to map a file on my local linux host /home/rkadmin/Apps/docker/pg/pg2/postgresql.conf to the file /etc/postgresql/postgresql.conf on the container. I have created a local file postgresql.conf where i have made the necessary configurations to the postgresql parameters that i want for my postgres database (eg: setting wal_level=‘logical’ to enable logical replication). The part of the command -c ‘config_file=/etc/postgresql/postgresql.conf’ tells the container to use the file as the postgres configuration file (because the file is in the non default location).
Now you can go ahead and access the database using psql. Once you login create the extensions.
psql -h localhost -d pg1db -U pg1user
psql> create extension pg_stat_statements;
psql> create extension vector;