Skip to content

Commit 060dad8

Browse files
authored
feat: adds new db package with postgres docker image (#661)
Signed-off-by: Anthony D. Mays <[email protected]>
1 parent a0cb9b0 commit 060dad8

File tree

15 files changed

+1237
-40
lines changed

15 files changed

+1237
-40
lines changed

lib/db/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM postgres:16-alpine
2+
3+
# Create a directory for initialization scripts
4+
RUN mkdir -p /docker-entrypoint-initdb.d
5+
6+
# Add any initialization scripts if needed
7+
# COPY ./init-scripts/ /docker-entrypoint-initdb.d/
8+
9+
# Set environment defaults (these can be overridden in docker-compose.yml)
10+
ENV POSTGRES_USER=postgres
11+
ENV POSTGRES_PASSWORD=postgres
12+
ENV POSTGRES_DB=mydb
13+
14+
# Expose the PostgreSQL port
15+
EXPOSE 5432
16+
17+
# Set the data directory
18+
VOLUME ["/var/lib/postgresql/data"]
19+
20+
# Set health check command
21+
HEALTHCHECK --interval=5s --timeout=5s --retries=5 \
22+
CMD pg_isready -U postgres || exit 1

lib/db/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# PostgreSQL Docker Setup
2+
3+
This directory contains scripts to easily start and stop a PostgreSQL database using Docker.
4+
5+
## Prerequisites
6+
7+
- [Docker](https://www.docker.com/products/docker-desktop/)
8+
- [Docker Compose](https://docs.docker.com/compose/install/) (included in Docker Desktop)
9+
10+
## Default Configuration
11+
12+
- **PostgreSQL Version**: 16 (latest stable)
13+
- **Port**: 5432
14+
- **Username**: postgres
15+
- **Password**: postgres
16+
- **Database**: mydb
17+
- **Data Volume**: postgres_data (persisted between restarts)
18+
19+
## Getting Started
20+
21+
Make the scripts executable:
22+
23+
```bash
24+
chmod +x start-postgres.sh stop-postgres.sh status-postgres.sh
25+
```
26+
27+
### Starting PostgreSQL
28+
29+
```bash
30+
./start-postgres.sh
31+
```
32+
33+
This script will:
34+
1. Start a PostgreSQL container
35+
2. Wait for it to be ready
36+
3. Display connection information
37+
38+
### Checking Status
39+
40+
```bash
41+
./status-postgres.sh
42+
```
43+
44+
This script will show if PostgreSQL is running and display connection details.
45+
46+
### Stopping PostgreSQL
47+
48+
```bash
49+
./stop-postgres.sh
50+
```
51+
52+
This script stops the PostgreSQL container while preserving your data.
53+
54+
## Connection Information
55+
56+
Once the database is running, you can connect to it using:
57+
58+
- **Host**: localhost
59+
- **Port**: 5432
60+
- **Username**: postgres
61+
- **Password**: postgres
62+
- **Database**: mydb
63+
64+
### Connect with psql client:
65+
66+
```bash
67+
psql -h localhost -U postgres -d mydb
68+
```
69+
70+
### Connect from applications:
71+
72+
Connection string format:
73+
```
74+
postgresql://postgres:postgres@localhost:5432/mydb
75+
```
76+
77+
## Customizing the Setup
78+
79+
You can modify the `docker-compose.yml` file to change:
80+
81+
- Database name, username, and password
82+
- Port mappings
83+
- Volume configuration
84+
- Add initialization scripts
85+
86+
## Data Persistence
87+
88+
The database data is stored in a Docker volume named `postgres_data` which persists between container restarts. To completely reset the data, you would need to remove this volume:
89+
90+
```bash
91+
docker volume rm db_postgres_data
92+
```
93+
94+
## Troubleshooting
95+
96+
If you encounter issues, check the container logs:
97+
98+
```bash
99+
docker-compose logs postgres
100+
```

lib/db/docker-compose.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3.8'
2+
3+
services:
4+
postgres:
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
container_name: 25q1_postgres_db
9+
ports:
10+
- "5432:5432"
11+
environment:
12+
POSTGRES_USER: postgres
13+
POSTGRES_PASSWORD: postgres
14+
POSTGRES_DB: mydb
15+
volumes:
16+
- postgres_data:/var/lib/postgresql/data
17+
# Uncomment to add init scripts
18+
# - ./init-scripts:/docker-entrypoint-initdb.d/
19+
restart: unless-stopped
20+
healthcheck:
21+
test: ["CMD-SHELL", "pg_isready -U postgres"]
22+
interval: 10s
23+
timeout: 5s
24+
retries: 5
25+
26+
volumes:
27+
postgres_data:
28+
driver: local
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- Sample initialization script
2+
-- This will run when the container first initializes
3+
4+
-- Create a sample table
5+
CREATE TABLE IF NOT EXISTS sample_table (
6+
id SERIAL PRIMARY KEY,
7+
name VARCHAR(100) NOT NULL,
8+
description TEXT,
9+
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
10+
);
11+
12+
-- Insert some sample data
13+
INSERT INTO sample_table (name, description)
14+
VALUES
15+
('Sample 1', 'This is a sample record'),
16+
('Sample 2', 'Another sample record');
17+
18+
-- Create a sample user (with limited privileges)
19+
-- Uncomment if needed
20+
/*
21+
CREATE USER sampleuser WITH PASSWORD 'samplepass';
22+
GRANT CONNECT ON DATABASE mydb TO sampleuser;
23+
GRANT USAGE ON SCHEMA public TO sampleuser;
24+
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO sampleuser;
25+
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO sampleuser;
26+
*/
27+
28+
-- Note: This script runs on database creation only. If you modify it,
29+
-- you'll need to remove the postgres_data volume to see the changes:
30+
-- docker volume rm db_postgres_data

lib/db/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "postgres-docker-setup",
3+
"version": "1.0.0",
4+
"description": "PostgreSQL Docker setup for development",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "./start-postgres.sh",
8+
"stop": "./stop-postgres.sh",
9+
"status": "./status-postgres.sh",
10+
"reset": "docker-compose down && docker volume rm db_postgres_data || true && ./start-postgres.sh",
11+
"logs": "docker-compose logs -f postgres"
12+
},
13+
"keywords": [
14+
"postgresql",
15+
"postgres",
16+
"docker",
17+
"database"
18+
],
19+
"author": "",
20+
"license": "ISC"
21+
}

lib/db/start-postgres.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
# Print colorful message
4+
echo -e "\033[0;32m[INFO] Starting PostgreSQL database...\033[0m"
5+
6+
# Navigate to the script directory
7+
cd "$(dirname "$0")"
8+
9+
# Check if Docker is running
10+
if ! docker info > /dev/null 2>&1; then
11+
echo -e "\033[0;31m[ERROR] Docker is not running. Please start Docker and try again.\033[0m"
12+
exit 1
13+
fi
14+
15+
# Start the PostgreSQL container
16+
docker-compose up -d
17+
18+
# Wait for PostgreSQL to be healthy
19+
echo -e "\033[0;33m[INFO] Waiting for PostgreSQL to be ready...\033[0m"
20+
attempt=0
21+
max_attempts=30
22+
23+
while [ $attempt -lt $max_attempts ]; do
24+
if docker-compose exec postgres pg_isready -U postgres > /dev/null 2>&1; then
25+
echo -e "\033[0;32m[SUCCESS] PostgreSQL is ready!\033[0m"
26+
echo -e "\033[0;34m
27+
Connection Information:
28+
Host: localhost
29+
Port: 5432
30+
Username: postgres
31+
Password: postgres
32+
Database: mydb
33+
34+
Connect with psql: psql -h localhost -U postgres -d mydb
35+
\033[0m"
36+
exit 0
37+
fi
38+
39+
attempt=$((attempt+1))
40+
echo -e "\033[0;33m[INFO] Waiting for database to be ready... ($attempt/$max_attempts)\033[0m"
41+
sleep 1
42+
done
43+
44+
echo -e "\033[0;31m[ERROR] Failed to connect to PostgreSQL after $max_attempts attempts.\033[0m"
45+
echo "Check container logs with: docker-compose logs postgres"
46+
exit 1

lib/db/status-postgres.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# Print colorful message
4+
echo -e "\033[0;34m[INFO] Checking PostgreSQL database status...\033[0m"
5+
6+
# Navigate to the script directory
7+
cd "$(dirname "$0")"
8+
9+
# Check if Docker is running
10+
if ! docker info > /dev/null 2>&1; then
11+
echo -e "\033[0;31m[ERROR] Docker is not running. Please start Docker and try again.\033[0m"
12+
exit 1
13+
fi
14+
15+
# Get container status
16+
CONTAINER_ID=$(docker-compose ps -q postgres)
17+
18+
if [ -z "$CONTAINER_ID" ]; then
19+
echo -e "\033[0;33m[INFO] PostgreSQL container is not running.\033[0m"
20+
exit 0
21+
fi
22+
23+
# Check if container is running
24+
if docker ps -q --no-trunc | grep -q "$CONTAINER_ID"; then
25+
echo -e "\033[0;32m[INFO] PostgreSQL container is running.\033[0m"
26+
27+
# Check if PostgreSQL is responsive
28+
if docker-compose exec postgres pg_isready -U postgres > /dev/null 2>&1; then
29+
echo -e "\033[0;32m[SUCCESS] PostgreSQL server is accepting connections.\033[0m"
30+
31+
# Display container info
32+
echo -e "\033[0;34m\nContainer Information:\033[0m"
33+
docker ps --filter "id=$CONTAINER_ID" --format "ID: {{.ID}}\nName: {{.Names}}\nStatus: {{.Status}}\nPorts: {{.Ports}}\n"
34+
35+
echo -e "\033[0;34m\nConnection Information:
36+
Host: localhost
37+
Port: 5432
38+
Username: postgres
39+
Password: postgres
40+
Database: mydb
41+
42+
Connect with psql: psql -h localhost -U postgres -d mydb\033[0m"
43+
else
44+
echo -e "\033[0;31m[WARNING] PostgreSQL container is running but the server is not accepting connections.\033[0m"
45+
fi
46+
else
47+
echo -e "\033[0;31m[ERROR] PostgreSQL container exists but is not running.\033[0m"
48+
echo "Container status:"
49+
docker ps -a --filter "id=$CONTAINER_ID" --format "ID: {{.ID}}\nName: {{.Names}}\nStatus: {{.Status}}\n"
50+
fi

lib/db/stop-postgres.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
# Print colorful message
4+
echo -e "\033[0;33m[INFO] Stopping PostgreSQL database...\033[0m"
5+
6+
# Navigate to the script directory
7+
cd "$(dirname "$0")"
8+
9+
# Check if Docker is running
10+
if ! docker info > /dev/null 2>&1; then
11+
echo -e "\033[0;31m[ERROR] Docker is not running. Please start Docker and try again.\033[0m"
12+
exit 1
13+
fi
14+
15+
# Stop the PostgreSQL container
16+
docker-compose down
17+
18+
# Check if the stop was successful
19+
if [ $? -eq 0 ]; then
20+
echo -e "\033[0;32m[SUCCESS] PostgreSQL database stopped successfully.\033[0m"
21+
else
22+
echo -e "\033[0;31m[ERROR] Failed to stop PostgreSQL database.\033[0m"
23+
exit 1
24+
fi
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Database Seeding Guide
2+
3+
This guide explains how to use the seed script to populate your database with sample data for development and testing.
4+
5+
## What is Database Seeding?
6+
7+
Database seeding is the process of populating a database with initial data. It's particularly useful for:
8+
- Development environments
9+
- Testing
10+
- Providing sample data for demonstrations
11+
- Setting up initial state for applications
12+
13+
## Seed Data Contents
14+
15+
The seed script populates the database with:
16+
17+
- **Todos**: 12 sample todo items spread across 3 different users
18+
- **Logs**: A single log entry recording the seed script execution
19+
20+
## How to Run the Seed Script
21+
22+
You can run the seed script using npm:
23+
24+
```bash
25+
npm run db:seed
26+
```
27+
28+
Or directly using ts-node:
29+
30+
```bash
31+
npx ts-node prisma/seed.ts
32+
```
33+
34+
## When to Run the Seed Script
35+
36+
You should run the seed script:
37+
38+
1. After setting up a fresh database
39+
2. After clearing your development database
40+
3. When you want to reset your database to a known state
41+
42+
## Modifying Seed Data
43+
44+
If you need to modify the seed data, edit the `/prisma/seed.ts` file. The script is structured as follows:
45+
46+
1. Define sample data in arrays
47+
2. Use Prisma Client to insert the data into the database
48+
3. Log the results
49+
50+
Feel free to add more sample data or modify existing data to suit your development needs.
51+
52+
## Important Notes
53+
54+
- The seed script will delete all existing todos before adding new ones
55+
- The script adds a log entry to record its execution
56+
- User IDs are strings and do not need to exist in an authentication system for development purposes

0 commit comments

Comments
 (0)