2023-04-06

Building a PostGIS Docker Container

Building a PostGIS Docker Container

In this article, I will introduce how to set up PostGIS using Docker. By utilizing the official Docker image provided below, you can easily launch a PostgreSQL Docker container with PostGIS pre-installed.

https://registry.hub.docker.com/r/postgis/postgis/

To start the PostGIS container, execute the following command:

$ docker run --name my-postgis -e POSTGRES_PASSWORD=mysecretpassword -d postgis/postgis

To enter the container and connect to PostgreSQL, use the following command:

bash
$ docker exec -it my-postgis psql -U postgres

postgres=#

Now, you have successfully connected to PostgreSQL.

PostGIS Operations

Let's perform some basic operations with PostGIS. First, we'll create an airports table.

bash
postgres=# create table airports
            ( id serial primary key,
              name text,
              iata_code text,
              location geography(POINT, 4326)
            );

CREATE TABLE

Next, let's insert data into the airports table.

bash
postgres=# insert into airports (name, iata_code, location)
            values ( 'Haneda Airport' , 'HND' , ST_GeogFromText('SRID=4326;POINT(139.7776446 35.5493975)') );

INSERT 0 1

Now, let's check the airports table.

bash
postgres=# select * from airports;

 id |      name      | iata_code |                      location
----+----------------+-----------+----------------------------------------------------
  1 | Haneda Airport | HND       | 0101000020E6100000279DED76E2786140888043A852C64140
(1 row)

Let's try converting the location column to the POINT type.

bash
postgres=# select id, name, iata_code, ST_AsText(location) from airports;

 id |      name      | iata_code |           st_astext
----+----------------+-----------+-------------------------------
  1 | Haneda Airport | HND       | POINT(139.7776446 35.5493975)
(1 row)

Lastly, let's calculate the distance from the point (139.00 35.00).

bash
postgres=# select ST_Distance(
  location,
  ST_GeogFromText('SRID=4326;POINT(139.00 35.00)')
  ) as distance from airports;

    distance
----------------
 93386.17699288
(1 row)

References

https://registry.hub.docker.com/r/postgis/postgis/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!