Run PostgreSQL + PGAdmin  in pods using podman
Tags: postgres, linux, docker, sql
Published: 2020-12-13 16:47:43

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](https://hub.docker.com/_/postgres/) and [pgadmin](https://hub.docker.com/r/dpage/pgadmin4/) together and I won't need to restart each separately.


### 1. Create a pod:

```
podman pod create --name learn-sql -p 9876:80
```
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
```
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
```
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
```

> 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
```


### 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][step1]

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

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][step3]

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

[step1]: https://i.imgur.com/vEVtRtF.png
[step2]: https://i.imgur.com/jHyyC36.png
[step3]: https://i.imgur.com/ywc970Y.png
[step4]: https://i.imgur.com/QXqbxZF.png

### 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
```

To start stop the pod
```
podman pod start learn-sql
podman pod stop learn-sql
```

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
```


That's it, until next time.