DEV Community 👩‍💻👨‍💻

Cover image for Run PostgreSQL + PGAdmin  in pods using podman
pr0PM
pr0PM

Posted on • Updated on

Run PostgreSQL + PGAdmin in pods using podman

To learn SQL, I decided to use postgreSQL and pgAdmin.
Now I had 2 options install it the simple way on my linux machine, or let's learn podman some more.

So I decided to use pods to keep both postgres and pgadmin together and I won't need to restart each separately.

1. Create a pod:

podman pod create --name learn-sql -p 9876:80
Enter fullscreen mode Exit fullscreen mode

Output will be similar to this:

pr0PM at ~  
podman pod create --name=learn-sql -p 9876:80
bdef801c5678e4f2657f680628efb7b4ce18920ab8e86fc0a706176ca05956f6
pr0PM at ~  
podman pod ps
POD ID        NAME       STATUS   CREATED        INFRA ID      # OF CONTAINERS
bdef801c5678  learn-sql  Created  6 seconds ago  665b4a4b687c  1
Enter fullscreen mode Exit fullscreen mode

This will create a pod that will house both our containers in podman and manage them.
We exposed port 9876 in the pod and map it to port 80 that we will use to access the pgadmin UI.

2. Create and run the pgadmin and postgres container inside the pod we just created:

podman run --pod=learn-sql \
-e 'PGADMIN_DEFAULT_EMAIL=pr0pm@pm.me' \
-e 'PGADMIN_DEFAULT_PASSWORD=pr0pm' \
--name pgadmin12 \  # name the container
-d dpage/pgadmin4   # the image we are pulling
Enter fullscreen mode Exit fullscreen mode

Ignore the silly creds there and note that we had already mapped the port 80 while creating the pod.

podman run --pod=learn-sql \
-v ~/your/data/volume:/var/lib/postgresql/data:Z \
-e POSTGRES_PASSWORD=pr0pm \
-e POSTGRES_USER=pr0pm \
--name db \
-d postgres
Enter fullscreen mode Exit fullscreen mode

Note: Use :Z in the end to modify the SELinux label in fedora 33 or you might be greeted with permission error, you might not need it if your distro doesn't follows SELinux by default.

3. View the current status of the containers running in your pod:

podman pod stats learn-sql

and results will look something like this:

POD           CID           NAME                CPU %  MEM USAGE/ LIMIT   MEM %  NET IO   BLOCK IO  PIDS
bdef801c5678  12dfba476985  pgadmin12           1.44%  171.1MB / 4.079GB  4.20%  -- / --  -- / --   11
bdef801c5678  665b4a4b687c  bdef801c5678-infra  3.00%  749.6kB / 4.079GB  0.02%  -- / --  -- / --   1
bdef801c5678  81422268e4d7  db                  8.00%  46.41MB / 4.079GB  1.14%  -- / --  -- / --   7
Enter fullscreen mode Exit fullscreen mode

4. Configure pgadmin to use postgres

Fire up your browser and head to 0.0.0.0:9876

Use the credentials you used while making the pgadmin container in the previous steps to login and you will be greeted with a screen like this:

img

Now click on the Add Server button and you will be presented with a menu like similar to the one below
img

Enter a name for your DB server and move to the next tab where you will add the host address, port, username and password.
img

Save it and connect to the running postgres container in the pod and check out your containerised postgres + pgadmin instance on your PC.
img

5. Useful operations

You can perform start/stop pause/unpause operations on the pod to work/resume/stop and all the data will remain persistent in our volume.

To pause/unpause the pod

podman pod pause learn-sql
podman pod unpause learn-sql
Enter fullscreen mode Exit fullscreen mode

To start stop the pod

podman pod start learn-sql
podman pod stop learn-sql
Enter fullscreen mode Exit fullscreen mode

If you wish to to generate the config with all the stuff ready for you to reproduce later

podman generate kube learn-sql >> learn-sql-pod-conf.yml
Enter fullscreen mode Exit fullscreen mode

That's it, until next time.

Top comments (1)

Collapse
chchrist profile image
Tolis Christomanos • Edited on

Thank you for this article!

I encountered two problems.

I can't mount the local folder. This is a know issue apparently discussed here github.com/containers/podman/issue...

I can't get it to work from the cli with psql. I exposed 5432 as well but it does nothing...
Fixed I exposed the port correctly -p 5432:5432

🌚 Life is too short to browse without dark mode