How to install PostGIS, PgAdmin, and PostgreSQL in Docker

PostgreSQL must be one of the most used and advanced SQL Servers. PostGIS can make our PostgreSQL installation more powerful adding spatial geometry and PgAdmin 4 is the perfect companion for both of them. Now let’s install them in Docker Containers.

PostgreSQL image

Contents

Installing Docker

First of all, we need to install docker on our PC/Server, thankfully, we already cover this topic in another post.

Just as a reminder, our Docker environment would be different if we used Linux, Mac or Windows, therefore, installation guides are a bit different for any of this environments.

Installing PostgreSQL

PostgreSQL has an image for Docker as many other SQL servers, we could install PostgreSQL by creating a docker-compose.yml file with this content:

version: '2'
services:
postgres:
image: 'postgres:latest'
restart: always
volumes:
- './postgres_data:/var/lib/postgresql/data'
environment:
- POSTGRES_PASSWORD=PASSWORD_HERE
ports:
- '5432:5432'

Then we would go to the folder containing the file through our preferred console and run:

docker-compose up -d

And ready! You would have your docker container set with PostgreSQL in it…

But what about PgAdmin and PostGIS? These are powerful tools that can be connected to PostgreSQL.

Running a Docker container with PgAdmin

The docker-compose.yml file that would make this possible would be:

version: '3.8'
services:
  pgadmin:
    container_name: PgAdmin_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: root@root.com
      PGADMIN_DEFAULT_PASSWORD: PASSWORD_HERE
    ports:
      - "5050:80"

This will run two nested containers

PostgreSQL Nested containers

You would access PgAdmin by going to HTTP://127.0.0.1:5050/ and you would have to connect to your PostgreSQL instance by using your computer LAN IP address (To see it in windows write IPCONFIG in CMD, in Linux IFCONFIG) (Because localhost would be the container)

Running PostgreSQL and PgAdmin in Docker

If we would want to run PostgreSQL and PgAdmin together, our docker-compose.yml would look like this:

version: '3.8'
services:
  db:
    container_name: Postgres_Server
    image: 'postgres:latest'
    restart: always
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: PASSWORD_HERE
      POSTGRES_DB: DATABASE_NAME_HERE
    ports:
      - "5432:5432"
    volumes:
      - './postgres_data:/var/lib/postgresql/data'
  pgadmin:
    container_name: PgAdmin_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: root@root.com
      PGADMIN_DEFAULT_PASSWORD: PASSWORD_HERE
    ports:
      - "5050:80"

How to Install PostGIS in Docker

First, let’s start by stating that PostGIS is an extension of PostgreSQL, we could either install it in our PostgreSQL installation or we could use the PostGIS docker image, in the second case, our docker-compose.yml would look something like this:

version: '2'
services:
postgres:
image: 'postgis/postgis:latest'
restart: always
volumes:
- './postgres_data:/var/lib/postgresql/data'
environment:
- POSTGRES_PASSWORD=PASSWORD_HERE
ports:
- '5432:5432'

This will install our docker container with PostgreSQL and PostGIS, which will convert our PostgreSQL installation into a spatial database.

How to install PostGIS, PostgreSQL and PgAdmin 4 in Docker

Finally, we will combine our codes to generate two containers with PostGIS, PostgreSQL and PgAdmin 4 our docker-compose.yml will be:

version: '3.8'
services:
  db:
    container_name: Postgres_Server
    image: 'postgis/postgis:latest'
    restart: always
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: PASSWORD_HERE
      POSTGRES_DB: DATABASE_NAME_HERE
    ports:
      - "5432:5432"
    volumes:
      - './postgres_data:/var/lib/postgresql/data'
  pgadmin:
    container_name: PgAdmin_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: root@root.com
      PGADMIN_DEFAULT_PASSWORD: PASSWORD_HERE
    ports:
      - "5050:80"

As you can see, we are creating nested containers as we do when installing PostgreSQL and PgAdmin together, but in this case, we are using the image of PostGIS (That comes with PostgreSQL) instead of the PostgreSQL image.

If you skip to the end, after doing this docker-compose.yml you should go to your preferred console and run:

docker-compose up -d

Conclusion

Docker is rather simple to use after you understand it well, here you have another post where we indagate each docker-compose file variable.

If you want to know more about PostGIS you can find it on PostGIS webpage which has a lot of documentation that you can relay on.

As always, hope you find this tutorial useful and see you on the next one!

Leave a Reply