PostGIS (Postgres-extended) set-up with Docker

PostGIS (Postgres-extended) set-up with Docker

PostGIS - ideal for geospatial data (google earth etc)

Motivaton

At work, I am getting tired of data management and I am looking for an ETL solution to forever alleviate the pain of internal data management. I am looking for a storage engine that can support geospatial datatype. After some research, I came across PostGIS.

What's PostGIS

PostGIS is a database extended from Postgres. It supports geo-spatial datatypes and it's popular among the GIS industry.

Some of the headache PostGIS is going to solve for me includes:

  • management of shapefiles (ESRI), geo-json and raster images.

  • interfacing with QGIS naturally (analytics could use it for storage)

  • directly query from the shapefile just like SQL (instead of converting shapefile with GDAL or geopandas to any readable format)

Set-up with Docker

You can find the official postgis/postgis on dockerhub. Let's use PostGIS with Pgadmin4.

Let's create a docker-compose.yml file by creating two services

version: "3.8"
services:
  postgis:
    image: postgis/postgis:15-3.3
    restart: always
    environment:
      POSTGRES_DB: <database-name>
      POSTGRES_USER: <username>
      POSTGRES_PASSWORD: <password>
    volumes:
      - ./postgisdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    container_name: postgis_container

  pgadmin4:
    container_name: pgadmin4_container
    image: dpage/pgadmin4:latest
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: admin
    volumes:
      - ./pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"
    links:
      - postgis

Then you run the following

# run docker compose in detached mode
docker compose up -d

Then you should have these two in docker desktop

Then you could open your favorite browser to go to http://localhost:5050

Then you do the following:

  • Log in with your Pgadmin credential

  • Set up a connection with Server

    • container_name = host_name, it can also be your container ip address
docker ps -a
# see the specs of a container and search for IP
docker container inspect <hashcode> | grep IP

You should have a list of key-value pairs with one being

{
    "IPAddress":"xxx.xx.x.x"
}

You could use this as your hostname as well. Then you should be able to see the monitoring dashboard of PostGIS.