Get Postgres ready in 5 min using Docker

Learn to setup and run Postgresql Database using Docker Container quickly and efficiently

Ashutosh Karna
4 min readJul 26, 2020
Photo by Felix M. Dorn on Unsplash

There is a need of Database in most of the software application and Postgres is one of the best option to choose. If we want to spend less time in installing, setting up and maintaining Postgres and focus on more time on software development, then running Postgres on Docker container is a great option. So, in this article, we will see how can we get up and running Postgres in 5 minutes. Let’s break down the whole process into following:

  1. Create docker-compose.yml file and required directories
  2. Run and Test the whole setup

Let’s go through them one by one.

  1. Create docker-compose.yml file and required directories

Assuming that you have already installed Docker and docker-compose, we will proceed forward to create docker-compose.yml file. If you have not installed them, then take some time and install it from here and here. Now, let’s create a file named docker-compose.yml in the project directory which will be the blue print for the container.

version: '3.1'

services:

postgresdb:
image: postgres:12.3
restart: always
volumes:
- ./postgres/data:/var/lib/postgresql/data
- ./postgres/logs:/logs
- ./postgres/config/postgresql.conf:/etc/postgresql.conf
environment:
POSTGRES_PASSWORD: <your_password>
POSTGRES_USER: <your_user>
POSTGRES_DB: test
ports:
- 5432:5432

In this file, we have specified service name for postgres as postgresdb and used 12.3 version of postgres, as specified in image’s tag. You can use any version of your choice, if you don’t specify tag, it will use latest version by default. In environment we can set user, password and database for postgres, as I have specified above. You can use your own values for this but the keys must be POSTGRES_PASSWORD , POSTGRES_USER and POSTGRES_DB . In ports we have mapped 5432 port of container with 5432 port of our local machine.

Finally, let’s discuss about volumes portion. We have two volumes, one for data and another for configuration. Data volume is very important as it enables us to keep the data of our database in our local machine, so that even if we stop or remove postgres container, our data is always safe with us and can be used again when we start container later. Also, volume for configuration is important since we want to have hold of our database’s configuration, so that we can change it later depending on our requirement. To get our volumes ready, let’s create a directory named postgres inside our project directory, and inside this, create two directories data and config . There is no need of adding anything in data directory since postgres will write everything needed here, but for config , we need to add configuration file here, so that when we start container, it can use this. So, inside config add file named postgresql.conf and you can add sample configuration from here.
Note: in conf, you must set listen_addresses = '*'so that other containers will be able to access postgres.
Now, the whole setup for volumes is ready, so we can just map the volumes, as specified in the file above. The ./postgres/data of our local machine has been mapped with /var/lib/postgresql/data inside the container and so on for configuration also. We are all set for running this, so let’s jump to next step.

2. Run and Test the whole setup

There are few commands which will help us to get our postgres container running. First will be

docker-compose ps

This is not mandatory but running this will confirm us that our docker-compose file has no errors since it will throw error if there is one. Remember you need to run these commands from same directory where this docker-compose.yml file is present, which is project directory in our case. Next, we will run following command:

docker-compose up -d

This command will pull the image if not available locally and then run the postgres container according to specification in docker-compose.yml file. I have used detached mode by specifying -d so that container will not stop if you exit and keep on running in background. After successful run of this command, our postgres is ready to use. We can also check the logs of the container using:

docker-compose logs -f postgresdb

Here, postgresdb is the name of service which we have specified.

Now, to test if our postgres is up and running or not, there are multiple ways. Here, we will try to use pgadmin to do this, which is simple gui tool for postgres. You can install pgadmin from here. Now, go to pgadmin and create new server with following specification:

host: localhost
username: postgres
database: test
password: helloworld1!

Now, we are connected to our new database, which is ready to use.

I guess we have reached to the end of article. Hoping that you will find this useful. All the codes and files associated with this article can be found in github repository.

Thanks for reading. Comments and suggestions are always welcome. Bye!

--

--