Skip to content

Latest commit

 

History

History
108 lines (74 loc) · 2.15 KB

postgres-setup.md

File metadata and controls

108 lines (74 loc) · 2.15 KB

PostgreSQL Database Setup

This document explains how to set up and run the PostgreSQL database using Docker for development and testing purposes.

Prerequisites

  • Docker
  • Docker Compose

Configuration

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

Environment Variables

Variable Description Current Value
POSTGRES_DB Database name safeswap
POSTGRES_USER Database user root
POSTGRES_PASSWORD Database password 12345678
POSTGRES_PORT Database port 5433

Getting Started

  1. Start the database:

    docker compose up -d
  2. Verify the database is running:

    docker compose ps
  3. Connect to the database from inside the container:

    docker exec -it project_db psql -U testuser -d testdb
  4. Connect to the database from your host machine:

    psql -h localhost -p 5433 -U testuser -d testdb

Data Persistence

The database data is persisted through a Docker volume named safeswap_data. This ensures your data survives container restarts and removals.

Stopping the Database

To stop the database:

docker compose down

To stop the database and remove all data:

docker compose down -v

Troubleshooting

If you encounter connection issues:

  1. Check if the container is running:

    docker compose ps
  2. View container logs:

    docker compose logs postgres
  3. Ensure the port is not already in use:

    sudo lsof -i :5433
  4. 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;
  5. If you need to reset and start fresh:

    docker compose down -v
    docker compose up -d