This document explains how to set up and run the PostgreSQL database using Docker for development and testing purposes.
- Docker
- Docker Compose
The database configuration is managed through environment variables. Create a .env
file in the apps/backend
root with the following configuration:
POSTGRES_DB=testdb
POSTGRES_USER=testuser
POSTGRES_PASSWORD=test@Pwd
POSTGRES_PORT=5433
Variable | Description | Current Value |
---|---|---|
POSTGRES_DB | Database name | safeswap |
POSTGRES_USER | Database user | root |
POSTGRES_PASSWORD | Database password | 12345678 |
POSTGRES_PORT | Database port | 5433 |
-
Start the database:
docker compose up -d
-
Verify the database is running:
docker compose ps
-
Connect to the database from inside the container:
docker exec -it project_db psql -U testuser -d testdb
-
Connect to the database from your host machine:
psql -h localhost -p 5433 -U testuser -d testdb
The database data is persisted through a Docker volume named safeswap_data
. This ensures your data survives container restarts and removals.
To stop the database:
docker compose down
To stop the database and remove all data:
docker compose down -v
If you encounter connection issues:
-
Check if the container is running:
docker compose ps
-
View container logs:
docker compose logs postgres
-
Ensure the port is not already in use:
sudo lsof -i :5433
-
Test database connection:
-- Once connected to psql, try: CREATE TABLE test (id serial PRIMARY KEY, name VARCHAR(50)); INSERT INTO test (name) VALUES ('test item'); SELECT * FROM test;
-
If you need to reset and start fresh:
docker compose down -v docker compose up -d