19

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.

user1685095
  • 5,787
  • 9
  • 51
  • 100
  • 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 Dec 18 '14 at 15:02
  • Also, it seems other people have done this: https://registry.hub.docker.com/u/mdillon/postgis/ – Adrian Mouat Dec 18 '14 at 15:03
  • Thanks for the comment. I've used some of their scripts, but It still doesn't work. Can you help? – user1685095 Dec 18 '14 at 15:34
  • 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 Dec 19 '14 at 13:07
  • No, it's postgis 2.1 and postgresql 9.3. It should work. – user1685095 Jan 20 '15 at 09:53
  • Ah, did you use the postgres base image then install postgis from apt-get? I don't think that will work - the postgres base image probably installs postgres by hand, not using apt-get. Look up the postgis installation instructions for installing from source/binary. – Adrian Mouat Jan 20 '15 at 10:21
  • Yes, I did. It would probably work, but I think that is stratge, that I basically should compile any extension to use it with postgres image. Shouldn't it be easy to use not the opposite? – user1685095 Jan 20 '15 at 11:41
  • I don't quite follow. If you want to use apt-get stuff, you will need to apt-get install postgres as well. Otherwise it won't be setup in the way debian expects. – Adrian Mouat Jan 20 '15 at 11:52
  • What if their image would be build using apt? If I understand what you're saying I would be able to install postgis with apt also? If so - this is obviously easier, right? – user1685095 Jan 20 '15 at 11:55
  • Yes, that should work but then you won't be able to use the official image and will always have to update the postgres image yourself. – Adrian Mouat Jan 21 '15 at 08:17
  • If theirs official image would be built with apt-get in dockerfile, I wouldn't be able to use official image? What do you mean? Official image installs postgres from their repo by the way, not from source code as I thought. Would I be able to use postgis If I would install it from that repo also? – user1685095 Jan 26 '15 at 08:09
  • The official image installs it from the *postgres* repo, not the debian repo, I doubt you can mix the two. – Adrian Mouat Jan 26 '15 at 09:20
  • By 'their' repo I meant posgtres repo. Any way the issue is not about repos it's about single user mode. Because I can create extensions from pgadmin succesfully. But I can't do that in single user mode. I'm starting to think that official docker images are... hhhm... not so good, you know. – user1685095 Jan 26 '15 at 09:30
  • Yeah, I dare say there are bugs. Open an issue on the tracker if you narrow it down to a specific problem. – Adrian Mouat Jan 26 '15 at 10:55

3 Answers3

11

---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;
macieks
  • 452
  • 6
  • 13
3

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
Alex M
  • 2,756
  • 7
  • 29
  • 35
Solway01
  • 61
  • 4
1

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.

LittleEaster
  • 527
  • 7
  • 10