How to create postgis extension for postgresql in docker?

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

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.


---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;