Create Postgres db with docker-compose on init"


I am new to Docker and am trying to create a docker-compose.yml file that will create a Postgres database when running docker-compose up. My docker-compose.yml file looks like this:

version: '3'

    image: postgres:latest
    restart: always
      - 5432:5432
      POSTGRES_PASSWORD: 'postgres'
      - database_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

    driver: local

The init.sql file contains only one line:


The log shows that the database system is ready to accept connections, but the database is not found. What could be the source of the problem?


The issue could be that the init.sql file is not being executed properly when the Postgres container is started. To fix this issue, you can try the following:

  1. Make sure that the init.sql file is in the same directory as the docker-compose.yml file.

  2. Check if the init.sql file has the correct file permissions. You can try running chmod +x init.sql to make it executable.

  3. Verify that the init.sql file is being mounted correctly as a volume in the docker-compose.yml file. The line - ./init.sql:/docker-entrypoint-initdb.d/init.sql should be correct, assuming that the init.sql file is in the same directory as the docker-compose.yml file.

  4. Check if there are any errors in the init.sql file itself. Make sure that the SQL syntax is correct and that the database name is specified correctly.

If you have tried the above steps and the issue persists, you can try running the container and checking the logs for any error messages. You can run docker-compose up and then use docker-compose logs to view the container logs. Look for any errors related to the initialization of the database.

Hope this helps!