A simple w​a​y to set-up PostGIS with Docker locally

Objective: Install PostGIS and pgAdmin locally using docker

Time Required: 15 minutes

Prerequisite: docker and docker-compose installed

The cool elephant logo of PostgreSQL

Install database and software is often very troublesome. For installing PostGIS, first, you have to have Postgresql installed, then install the PostGIS extension, finally, you have to enable the extension. You get the point…

Using pre-build docker images makes it simple. All you have to do is have your docker file set up and docker-compose file ready to go. Then, build it container and bam! You are ready to connect to the database and do some work.

Step-by-step

First, clone this repository into your computer. Personally, I am used to having all the docker structure in my ~/Docker directory. Howeer, feel free to put it anywhere on your file system.

mkdir -p ~/Docker
cd ~/Docker
git clone https://gitlab.com/edmondklai/postgis-pgadmin.git
cd postgis-pgadmin
atom .

The above commands will make the Docker directory if it doesn’t exist. Next, change directory into the Docker directory. Then, clone the repository from gitlab. Lastly, change directory into newly clone repository, and open the directory in Atom (or you can use any other editor you like).

docker-compose file

Let’s briefly go over what each of the lines means in the docker-compose file. For more detail definition, visit the docker compose reference official documentation.

Docker-compose explained:

version: Specify which docker-engine you will be using, leaving it at 3 is fine. You can change it to work with specific docker engine.

services: Specify the container that I am creating. In our case, we have 2 services: postgis and pgadmin.

build: How do we build the image? What should be included in the container? If you look closely at the dockerfile, you can see that it is based on the postgres:alphine image. Then runs commands to install PostGIS extension.

services/volumes: Data within the container does not persist when the container is stopped. So we need to find some way to save the files onto the host’s file system. In our case, we are mapping the data onto a docker volume in the host called postgis_data. The volume is saved to the host machine and next time you launch the container, your data will be there!

environment: This section help defines some of the environment variables needed. It supplies the username and password when the database and pgadmin are set up.

ports: We are mapping port 80 from to the docker container to port 80 to our host machine. So when we access our localhost, we can access the container.

depends_on: In our case, pgAdmin depends on the PostGIS database. Docker compose knows that it needs to start the database first then start the pgAdmin service.

volumes: It creates a docker volume to store the data

Launching the container

In the terminal, type in the following. The -d detached the output from your terminal. You can launching it without -d to see the output.

docker-compose up -d

The command will start building the image and then launch the container. At this point, hopefully you are seeing something like this.

Building the image….

Logging in to pgAdmin

Now, that both PostGIS and pgAdmin is up. We can connect to the pgAdmin. In your favorite browser, type in ‘localhost’ in your address bar and hit enter.

If you can see this page, congratulation!

Connect to the PostGIS database

Once you logged into pgAdmin, you will be greeted by their home page, which looks like the following image. The next step is to connect pgAdmin to our PostGIS database. Right-click on Server > Create > Server. Then fill out the connection information. The following images provide a guide on how to fill out the connection information.

click, click, click
Don’t be like me, give it a fun name
Fill it out based on your docker-compose file

Host name/address is the service of your docker-compose. In our case, it is ‘postgis‘.

Port can be leave to the default 5432.

Username and password is the PostGIS user and password in the compose file, in our case, it is ‘admin’ and ‘password’.

Hit Save and you are connected!

Conclusion

This guide went over how to set up your own PostGIS database locally and connect to it with pgAdmin. The docker-compose file can be edited to suit your production needs. The container approach of setting up the database is more flexible and portable compare to the traditional approach. Any question or comment, feel free to comment below.

Share the knowledge

1 comment / Add your comment below

Leave a Reply

Your email address will not be published. Required fields are marked *