How to install postgresql on docker
You have succeeded in setting up docker engine in Fedora 32 and installing docker compose as well.
Now it is time to install postgresql as a docker container.
But first things first, Why install postgresql as a docker container rather than the usual popular way?
Reasons for containerization of applications/databases.
- Software installation is quite chaotic especially if you have various versions running in your system. version clashes, upgrades and alot of productive time lost in this mundane task.
- Container images launches faster as compared to applications installed directly in your Operating System
- In this age of DevOps Culture,a methodology defined by high levels of interactivity between various stakeholders(DevOps Engineers, QA Automation Engineers and of, course Developers),the abstraction actually enhances the flexibility, portability and hence the agility of these teams in terms of delivery.
- From a personal experience, this containerization has actually helped me alot when dealing with my highly partitioned OS, prior to that some of them e.g postgresql and plant-uml server were quite hard when configuring them.
Enough of that.Not convinced? Anyway, here is the process of setting up postgres as a docker container.
Step 1: Create a Docker Compose file and run it
Ensure the docker service is running, trigger this manually.
| |
1.1 Create a Docker Compose file
Here is your docker compose file configured with latest postgres
The docker-compose.yml file
| |
1.2 Create an environment variable file
Now database.env file
POSTGRES_USER=tester
POSTGRES_PASSWORD=test
POSTGRES_DB=family
Type the following command to run postgres docker container.Make sure the above files are in the same directory and the terminal is also pointing there before executing the following command.
| |
Expected output from terminal logs
| |
We are interested in this part database system is ready to accept connections
Step 2: Connect your running container with you database client
In my case,I usually use DBeaver, you can use pgAdmin or Adminer Client.
TODO: Actually you can use a docker container for the client e.g Adminer Docker Image but that is for another day.

Step 3: Create tables in your database and add some data
Right click PostgreSQL - family > SQL Editor .Paste and execute the following sql scripts.
3.1 Create tables
| |
3.2 Insert sample data
| |
3.3 Do some queries
| |
| |
After closing our database client and stopping the container, we should later confirm if indeed our data was persisted.
Conclusion
You have achieved the following:
- Created a Docker Compose file and an environment file as well, the run it.
- Connected your running container with your database client.
- Created tables in your database and added some data.
Having done that now we have chance to look into automating starting of some specific docker containers at system boot.
Troubleshooting
Sometimes you may find out that you did something to your database and can’t seem to fix it.You just wish to nuke everything and start on a clean slate.So it would be better to completely delete the docker volumes holding the databases and recreating them afresh.
Here are steps you can follow:
1. Delete all the containers
This is done prior to deleting the volumes.
| |
If it is running you can either stop it or forcefully delete it using the following command
| |
container_id, you will get it from the first command.
2. Delete all the volumes
| |
or be more specific if you don’t want to delete all the volumes
| |
| |
3. Recreate the volumes
Recreating is simply starting the application
| |
If using docker-compose.yml, you may as well recreate the database.
| |
The name database is actually one of our service in the docker-compose file(just look at it closely).
But before executing the above command you may need to check your environment variables.I had actually messed up on this one.
| |
You can try connecting to server using following command, verify the role tester here is actually the one appearing in the output of above command.
| |
If you need to kill the container docker-compose kill, then docker-compose rm to remove it.
Sometimes removing the images, containers etc, works so docker-compose down.