This demonstration shows both .sql
and .sh
files being used to initialize a database programatically. The commands used in the .sh
are not important, but they demonstrate the type of commands you might add to that file. The .sql
file shows some simple commands you might use to stand up your postgres db.
I've attached a pgadmin4 container to the compose stack. This is just for ease of use, it connects to the postgres db and displays a lot of useful information quickly. Feel free to remove if not wanted.
I've gone ahead and declared these important variables:
POSTGRES_USER: webmaster
POSTGRES_DB: docker
Then inside of the .sh
init script, there is a command to create a new user and grant privileges.
There's no reason why i've set them to these values, I am only demonstrating the variables available and highly pertinent.
Feel free to customize these variables, just ensure that if you change a db/table/user name, you update all references to that name.
i used a strange naming convention for the files, the docs say that the init scripts are ran in sorted order. I wanted the .sh file to be ran first so i forced it to be sorted first by adding a.*
to the name.
Ensure that the .sh file is executable. You can check with ls -la
while in the directory, and add if needed with chmod +x a.init-user-db.sh