This guide explains how to run a SQL command on the PostgreSQL database container used in the atlas-cmms Docker Compose setup.
- Docker and Docker Compose installed.
- The
atlas-cmmsstack up and running:
docker compose up -dTo execute a SQL command, first access the running PostgreSQL container:
docker exec -it atlas_db psql -U $POSTGRES_USER -d atlasatlas_db: Name of the container as defined indocker-compose.ymlpsql: The PostgreSQL CLI tool-U $POSTGRES_USER: Connects using the username from environment variables-d atlas: Connects to theatlasdatabase
If your environment variables are not exported, replace $POSTGRES_USER with your actual database username.
Once inside the PostgreSQL shell (psql), you can execute any SQL command. For example:
SELECT * FROM own_user;To exit the psql shell, type:
\qYou can also run a SQL command directly without entering the interactive shell:
docker exec -i atlas_db psql -U $POSTGRES_USER -d atlas -c "SELECT * FROM own_user;"Replace "SELECT * FROM own_user;" with your actual SQL command.
For more advanced database operations or bulk SQL files, consider mounting a .sql file into the container and executing it.
Assuming you have a file init.sql in your current directory:
docker cp init.sql atlas_db:/init.sql
docker exec -i atlas_db psql -U $POSTGRES_USER -d atlas -f /init.sql-
Ensure the database container is running:
docker ps
-
Check logs if connection fails:
docker logs atlas_db