I am trying to use an sh script to create a docker container with preloaded initial schemas. This means that I have a python main.py file that runs a bash script with the following code:
rm -rf postgres_data
docker-compose down --rmi all --volumes
docker-compose up --force-recreate
And a docker-compose.yml with the following code:
version: '3'
services:
postgres:
image: postgres:13.1
container_name: mydatabasename_database
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
timeout: 45s
interval: 10s
retries: 10
restart: always
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
- POSTGRES_DB=MYDATABASENAME_DB
- APP_DB_USER=appuser
- APP_DB_PASS=appass
- APP_DB_NAME=appname
volumes:
- ./init-schema.sql:/docker-entrypoint-initdb.d/init-db-01.sql
- postgres_data:/var/lib/postgresql/data
ports:
- 5432:5432
volumes:
postgres_data:
I also have an sql script called init-schema.sql with the following code:
CREATE USER appuser WITH PASSWORD 'appass';
CREATE DATABASE mydatabasename_db;
GRANT ALL PRIVILEGES ON DATABASE mydatabasename_db TO appuser;
\c mydatabasename_db;
CREATE TABLE IF NOT EXISTS my_table (
client_id character varying(36) NOT NULL,
value character varying(255)
);
Again, in the docker logs everything seems to be running as it should; however, when I try to connect to the container via dbeaver or pycharm, the table is non existent. All of these files are in the same directory by the way.
I have literally tried everything for the life of me. Initially, I tried simply using the docker build command and a Dockerfile to copy initial-schema.sql into the container directory via COPY ./initial-schema.sql /docker-entrypoint-initdb.d/config.sql, but this also did not work for some reason. Because of that, I instead moved to using the docker-compose plugin where I then wrote the code I provided above. I have no idea what else to do. I have also run the sql script manually and it works perfect. The table pops up in dbeaver and pycharm. I want everything to be loaded in a single step and this single error is what is left for me to accomplish that. I want to give you all thanks in advance! Hopefully, you guys can spot something that I am missing because I am completely out of ideas.
I fixed it myself! It seems that naming the section
postgreswas causing problems. Changed my file to this: