I have a docker compose file, which spins up postgres db. How can I run some psql commands after this container is started?
This is what I’m trying to run:
createuser postgres
createdb DB
createdb TEST_DB
psql -d DB -c "GRANT CREATE ON DATABASE "DB" TO postgres"
psql -d DB -c "CREATE EXTENSION IF NOT EXISTS "uuid-ossp""
psql -d DB -c "CREATE EXTENSION IF NOT EXISTS "pg_trgm""
psql -d TEST_DB -c "GRANT CREATE ON DATABASE "TEST_DB" TO postgres"
psql -d TEST_DB -c "CREATE EXTENSION IF NOT EXISTS "uuid-ossp""
psql -d TEST_DB -c "CREATE EXTENSION IF NOT EXISTS "pg_trgm""
2
Answers
postgres image initialization scripts did it! I used COPY in my Dockerfile to copy some .sh files to the container.
The official postgres image supports initialization scripts, you can save your commands in .sql file in /docker-entrypoint-initdb.d and they will be executed