I'm working with a simple postgres database and docker. Currently, I have a docker-compose file which creates the container I need and loads in the SQL files. After loading in this data, I would like it to perform a simple query through a bash script that I"m going to use for some basic tests (i.e., confirm # of rows > 0, etc). To start, I'm just trying to make a simple script that will run and print the number of rows (then I can worry about implementing actual testing). Here is what I have so far:
docker-compose.yml:
services:
postgres:
image: postgres
environment:
POSTGRES_DB: test-db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- ./database/create_table.sql:/docker-entrypoint-initdb.d/create_table.sql
- ./databases/data.sql:/docker-entrypoint-initdb.d/data.sql
- ./script/test.sh:/docker-entrypoint-initdb.d/test.sh
test.sh:
#!/bin/bash
echo "Accessing bash terminal of DB container"
docker exec -it postgres_1 bash
echo "Accessing psql terminal"
psql -U postgres
echo "Connecting to database"
\c database
echo "Checking number of rows"
numrows = $("SELECT count(*) FROM my_table")
echo numrows + " found."
Currently when I run docker-compose up, it creates the data from my SQL files and then stays idle. Is there something additional I need to run my script? I am able to do all of this myself through a separate terminal, but I would like this to all be automated so that I can just add tests to my test.sh and then run that rather than having to do it manually each time. What am I missing here? Shouldn't my script work since I really just recreated the commands I was executing manually? Thanks for any help!
psqlshell); trying to drive this through a standard database client library in a more structured programming language, either from the host or from a separate container, seems like it'd be a much more straightforward approach.psql -h localhost -c 'SELECT ...' databaseand then use a library like shUnit2 as a test framework (connecting to the container's published port; don't bind-mount the test script into the container and don't usedocker exec).