Created
September 5, 2016 09:17
-
-
Save onjin/2dd3cc52ef79069de1faa2dfd456c945 to your computer and use it in GitHub Desktop.
example docker compose for postgresql with db init script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
postgres: | |
image: postgres:9.4 | |
volumes: | |
- ./init.sql:/docker-entrypoint-initdb.d/init.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table sometable(id int); |
I was facing the same issue of init scripts getting ignored.
I HAVE FOUND A SOLUTION if it helps anyone !!
version: '2' services: common_db: image: postgres:10.4 restart: always environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres123 - POSTGRES_MULTIPLE_DATABASES=db1,db2 volumes: - ./db.sql:/docker-entrypoint-initdb.d/db.sql - ./postgres-data:/var/lib/postgresql/data ports: - 5436:5432 networks: - app-network volumes: postgres-data: networks: app-network: driver: bridge`
Now as you can see I have
postgres-data:
volume mounted. If i remove this directory before starting docker-compose file things work !!.I that directory/volume is present the init scripts are ignored !! enjoy!!
GOD!!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you put a CREATE DATABASE statement at the top of your actual SQL script, but don't supply POSTGRES_DB, it will use the POSETGRES_USER as the DB name so the tables don't go where they should.
If you supply both POSTGRES_DB and put a CREATE DATABASE statement at the top of the SQL script, you will get an error about the DB already existing since POSTGRES_DB will create the DB before the script runs. You can see this order of operations in the logs.
I think the real fix here is to: