[DB] Creating new databases #141

Closed
opened 2026-04-07 07:10:54 +00:00 by a24vinla · 7 comments
Collaborator

We need to figure out how to create new databases and run them through docker.

We need to figure out how to create new databases and run them through docker.
Author
Collaborator

Currently the compose file uses a standard postgres image, the documentation can be found here https://hub.docker.com/_/postgres .

Currently the compose file uses a standard postgres image, the documentation can be found here https://hub.docker.com/_/postgres .
Collaborator

So, we have two ways of creating and using databases through docker.

  1. Create new databases on the same container
    The first time a postgres container is started, every .sql and .sh file in the directory /docker-entrypoint-initdb.d is run. So you just link a .sql file to that directory (add the link to the postgres service volumes in compose.yaml) with "CREATE DATABASE ...;" for each additional database you want to create.
    NOTE: the database "sensors" is still created. Also, you need to remove the old volumes (eg. docker compose down -v) for it to run the files in /docker-entrypoint-initdb.d every time a new database is added.
    All databases created this way share the same password and ports.

  2. Create a new container for each database
    It is also possible to just add another service to the compose.yaml file that also uses the postgres image. Then the port and name have to be different from the first database, and you can also set a different password.
    Using different containers for the databases has a larger overhead, but the advantage is that the databases are more independent. Eg. Having all databases in the same container is a single point of failure. Using different containers, the one database can crash while the others continue running.

So, we have two ways of creating and using databases through docker. 1. Create new databases on the same container The first time a postgres container is started, every .sql and .sh file in the directory /docker-entrypoint-initdb.d is run. So you just link a .sql file to that directory (add the link to the postgres service volumes in compose.yaml) with "CREATE DATABASE ...;" for each additional database you want to create. NOTE: the database "sensors" is still created. Also, you need to remove the old volumes (eg. docker compose down -v) for it to run the files in /docker-entrypoint-initdb.d every time a new database is added. All databases created this way share the same password and ports. 2. Create a new container for each database It is also possible to just add another service to the compose.yaml file that also uses the postgres image. Then the port and name have to be different from the first database, and you can also set a different password. Using different containers for the databases has a larger overhead, but the advantage is that the databases are more independent. Eg. Having all databases in the same container is a single point of failure. Using different containers, the one database can crash while the others continue running.
Collaborator

Good explanation and correct understanding of both approches.
The use of docker-entrypoint-initdb.d is accurately described, especially that scripts only run on first initialization (when no volume exists), which is important.
The comparison between single container vs multiple containers is also clear and highlights trade-offs like simplicity vs isolation.

Also, note that the default database,e.g. "sensors" depends on the current configuration, not Postgres itself. Overall, this is correct and useful.

I’ve tested it now and it seems to work as intended. PostgreSQL starts correctly and both sensors and sensors_test are present, so from my side it looks ready to be merged.

Good explanation and correct understanding of both approches. The use of docker-entrypoint-initdb.d is accurately described, especially that scripts only run on first initialization (when no volume exists), which is important. The comparison between single container vs multiple containers is also clear and highlights trade-offs like simplicity vs isolation. Also, note that the default database,e.g. "sensors" depends on the current configuration, not Postgres itself. Overall, this is correct and useful. I’ve tested it now and it seems to work as intended. PostgreSQL starts correctly and both sensors and sensors_test are present, so from my side it looks ready to be merged.
Collaborator

Review
I have created test databases using both methods and it works well.
If this is supose to show what methods are avalible it's good, a google search with the information avalible in the document can lead you to the solution.
But if it's a guide to impliment these some things can be added to help with setting up the databases.
on way 1. the first time a new db is setup some files needs to be creaeted, after the first time its done its just as you have described.
on way 2. I beleve the diffrent dbs need sepperate volumes and then you also have to add at the bottom of the .yaml files the new volume.

I think this can be put in the wiki as is and if a guide should be made it can be a new issue to expand on this.

**Review** I have created test databases using both methods and it works well. If this is supose to show what methods are avalible it's good, a google search with the information avalible in the document can lead you to the solution. But if it's a guide to impliment these some things can be added to help with setting up the databases. on way 1. the first time a new db is setup some files needs to be creaeted, after the first time its done its just as you have described. on way 2. I beleve the diffrent dbs need sepperate volumes and then you also have to add at the bottom of the .yaml files the new volume. I think this can be put in the wiki as is and if a guide should be made it can be a new issue to expand on this.
Collaborator

I think if we are to put this in the wiki, we should choose one "standard" way that we use to add new databases (team leader's choice). In my uneducated opinion, we should go with option 1.
We already have a branch that implements option 1 (adds the necessary files) and a test database, so the wiki would only have to include an explanation of how it works and how to add new databases.

I think if we are to put this in the wiki, we should choose one "standard" way that we use to add new databases (team leader's choice). In my uneducated opinion, we should go with option 1. We already have a branch that implements option 1 (adds the necessary files) and a test database, so the wiki would only have to include an explanation of how it works and how to add new databases.
Collaborator

I agree with option 1, after the files are created its easy to make new dbs, and i dont see a reason why we need diffrent passwords for the dbs. But checking with the teamleader can be a good idea.

I agree with option 1, after the files are created its easy to make new dbs, and i dont see a reason why we need diffrent passwords for the dbs. But checking with the teamleader can be a good idea.
Author
Collaborator

I believe it might be good to document both, as having databases that don't exist in the same volume could be useful. Although it isn't necessary right now, having different ports and passwords for databases will definitely be required later on.

Will close this issue.

I believe it might be good to document both, as having databases that don't exist in the same volume could be useful. Although it isn't necessary right now, having different ports and passwords for databases will definitely be required later on. Will close this issue.
Sign in to join this conversation.
No milestone
No project
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Andras/BoundlessFlowCampus2K#141
No description provided.