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

0
26
Asked By CuriousCat42 On

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!

3 Answers

Answered By CynicalSteve On

Let's be real, ChatGPT isn’t always accurate, even with tech stuff.

SkepticalSammy -

For sure! It has its flaws.

Answered By StraightTalkSam On

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.

Answered By HelpfulHarry99 On

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!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.