Why isn't my Docker Compose setup creating the databases?

September 20, 2025 3 answers
Asked by CuriousCat42
Hey everyone! I've set up a Docker Compose file for my application which consists of a frontend, backend, and five PostgreSQL databases (one main and four external databases). I tried to make this database setup, expecting the Docker service to create everything automatically, but I keep seeing errors that say the databases do not exist. For example, I get messages like "database 'mainuser' does not exist" and more for the external databases. I've tried restarting everything, deleting old volumes, and even building the containers again, but nothing seems to help. I did manage to go into each container and create the databases manually, and that worked fine. So, I'm wondering, why didn't Docker create those databases on its own initially? Shouldn't it have created them on startup? Any insights would be appreciated! Thanks!

Answers

HelpfulHarry99
I hear you! The problem is that your Docker Compose file doesn't include any commands to automatically create the databases or users when the containers start. You're just starting the containers without any initialization scripts. I once added a SQL file to my MariaDB container like this: `- "./sqluser.sql:/docker-entrypoint-initdb.d/1.sql"`, which populated the database when it launched. You should look into that option. Here's more info on pre-seeding your database: [Docker Docs](https://docs.docker.com/guides/pre-seeding/#pre-seed-the-database-by-bind-mounting-a-sql-script). But now you know and can avoid that issue going forward!

InquisitiveJade: I tried something similar too, but it didn't work. I realized now that I needed to pre-seed the databases. Thanks for the clarification!

AvoidingErrors: Pre-seeding definitely makes a big difference!

Reply to this answer
StraightTalkSam
A couple of things here. First off, let's establish that "volms" isn't a term anyone will recognize. Just stick to the right jargon to avoid confusion. On the other hand, using AI tools blindly can lead to bad practices. It's better to understand what youโ€™re doing instead of just following the advice you get, even from ChatGPT. Regarding your issue, the trick lies in how the PostgreSQL images are configured. Check out this section in the Docker documentation about [initialization scripts](https://hub.docker.com/_/postgres#initialization-scripts). That might give you some clarity!

CuriousCat42: Thanks for the heads-up! I'll remember to use the right terms.

Reply to this answer
CynicalSteve
Let's be real, ChatGPT isnโ€™t always accurate, even with tech stuff.

SkepticalSammy: For sure! It has its flaws.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions