How to Install PostGIS and PgAdmin4 with Docker Easily
Tags: Postgis, Docker, Postgresql, Data Science, GIS

We will start with Docker Installation.

Install Docker

Even if you do not have docker, no problem, it has never been easier to install Docker for Windows or any other operating system. I am using Windows, so that I will download Docker for Windows.

The downloading and installation process is straightforward. Just download the docker desktop for your operating system and double click to run the installation.

Install PostgreSQL 12

This section will install PostgreSQL 12, mount a local folder to hold the data, and set up the PostgreSQL environment.

First, let us open a terminal and create a folder in your local machine. I will do that on the desktop and name it Postgres.

mkdir postgres

cd postgres

This folder stores the data locally as Docker containers do not persist beyond the lifecycle of the container. So next, we need to mount a volume inside this folder.

docker volume create --driver local --name=pgvolume

Next, we need to set up environment parameters. We will create a file pg-env. list, to hold these parameters. You can use any text editor to create this file and save it. Later we will pass them when we are running the PostgreSQL docker image.

PG_MODE=primary

PG_PRIMARY_USER=postgres

PG_PRIMARY_PASSWORD=postgres

PG_DATABASE=postgisdb

PG_USER=pgisdb

PG_PASSWORD=postgres

PG_ROOT_PASSWORD=postgres

PG_PRIMARY_PORT=5432

These parameters are user name, password, a PostGIS database, and the port. Note that you can change these values. Forexample, if you have a local PostgreSQL running at port 5432, take any other port.

Next, we run the following command to create a network bridge that will connect PostgreSQL 12 and PgAdmin 4 containers.

docker network create --driver bridge pgnetwork

Finally, we run the PostgreSQL docker container and pull it locally.

docker run — publish 5432:5432 — volume=pgvolume:/pgdata — env-file=pg-env.list — name=postgres — hostname=postgres — network=pgnetwork — detach crunchydata/crunchy-postgres-gis:centos7–12.5–3.0–4.5.1

Note that we are passing the pg-env.list, the mounted volume, and the network bridge. Also, we are pulling the crunchy Postgres gis image from docker.

The process will take time, and by the time the process ends, you have a running PostgreSQL database with PostGIS. In the next section, we will go through how to run Pgadmin 4 with Docker.

Install PgAdmin 4

Like the PostgreSQL process, we will create a volume and make parameters before running the PgAdmin 4 image. Run the following command:

docker volume create --driver local --name=pga4volume

Now, we also need to create the PgAdmin 4 environment. We can make another file pgadmin-env.list and complete the following configurations.

PGADMIN_SETUP_EMAIL=pgadmin PGADMIN_SETUP_PASSWORD=pgadmin SERVER_PORT=5050

PgAdmin 4 needs an email, password, and the server port. Note that you can pass any name here instead of email. We will also run the PgAdmin in port 5050.

So let us pull and run docker the docker image for PgAdmin 4.

docker run — publish 5050:5050 — volume=pga4volume:/var/lib/pgadmin — env-file=pgadmin-env.list — name=pgadmin4 — hostname=pgadmin4 — network=pgnetwork — detach crunchydata/crunchy-pgadmin4:centos7–12.5–4.5.1

It might take a while to pull the image. After the process ends, you have PgAdmin 4 and PostgreSQL containers running on your local machine. In the next section, we will configure it.

Configure PgAdmin 4

Finally, we can go to a browser and open a PgAdmin 4 Interface. So go ahead and run localhost:5050 in your browser.

Log in with your credentials we created in pgadmin-env.list the user and the password.

Next, we need to create a server. We will also pass the ports, username, and password we created in step 2 for the PostgreSQL installation. Refer to the video attached to configure your database and check the PostGIS extension.

Conclusion

In this step by step guide and the accompanying video, we have installed Docker for Windows and created a PostgreSQL database, PostGIS extension, and PgAdmin 4 with Docker.

I hope that helps you spin up an easy PostgreSQL database with Docker easily. Time to do some spatial analysis with PostGIS. Good luck.