18
votes

I'm getting errors when trying to create postgis extensions.

Here is what my dockerfile looks like.

    from postgres
RUN apt-get update && apt-get install postgis -y
ADD /create_postgis_extension.sh /docker-entrypoint-initdb.d/

create.bla-bla..sh

#!/bin/sh
POSTGRES="gosu postgres postgres"

$POSTGRES --single -E <<EOSQL
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
EOSQL

And here is the error when running the image

backend> statement: CREATE EXTENSION postgis;

ERROR: type addbandarg[] does not exist STATEMENT: CREATE EXTENSION postgis;

backend> statement: CREATE EXTENSION postgis_topology;

backend> ERROR: required extension "postgis" is not installed

I'm doing something wrong obviously, but I don't know what. Why is postgis in not installed if I've installed postgis with apt-get.

3
Is the error on Docker run? If not, when/how are you calling the script? Remember postgres won't be running until after the entrypoint/cmd script executes at runtime. - Adrian Mouat
Also, it seems other people have done this: registry.hub.docker.com/u/mdillon/postgis - Adrian Mouat
Thanks for the comment. I've used some of their scripts, but It still doesn't work. Can you help? - user1685095
Looks like incompatible versions of postgis/postgres to me, but I don't really know anything about postgis so I can't really help I'm afraid. I did notice the linked project uses specific versions of both postgis and postgres. - Adrian Mouat
No, it's postgis 2.1 and postgresql 9.3. It should work. - user1685095

3 Answers

6
votes

---DOCKERFILE

FROM postgres:12.4

RUN apt-get update \
    && apt-get install wget -y \
    && apt-get install postgresql-12-postgis-3 -y \
    && apt-get install postgis -y

COPY ./db.sql /docker-entrypoint-initdb.d/

--- db.sql (in this same folder)

CREATE EXTENSION postgis;
3
votes

I am using CentOS rather than Debian but ran into the same problem. The solution basically came down to using pg_ctl to start/stop postgres.

sudo -u postgres pg_ctl start -w -D ${PGDATA}

sudo -u postgres createdb postgis_template -E UTF8
sudo -u postgres psql -d postgis_template -c "create extension if not exists postgis;"

sudo -u postgres pg_ctl stop -w
1
votes

It is possible that you installed the wrong version of postgis for that postgres?
addbang[] exists from version 2.1.0, there was an issue around that between 2.0 and 2.1.
RedHat family has less of those issues due their behavior of getting updates less frequently (who go slow...)

That being said.
The docker-entrypoint-initdb.d is managed by docker-entrypoint.sh internal script , this runs only when the PGDATA folder do NOT exists.
This init script is able to manage some files: .sh, .sql and .sql.tar.gz.
Those are executed in alphabetic order by docker's user postgres.

Rather than use sh to do sql, use sql.
create_postgis_extension.sql:

CREATE EXTENSION IF NOT EXISTS POSTGIS;
CREATE EXTENSION IF NOT EXISTS POSTGIS_TOPOLOGY;

clean&simple

Regards.