Database Documentation
Documentation for the PostgreSQL database setup
Database Documentation
This document explains how the PostgreSQL database is configured and used in the Hosted Components application.
Technology Stack
- PostgreSQL: A powerful, open-source relational database
- Drizzle ORM: Type-safe ORM for TypeScript
Database Architecture
The application uses PostgreSQL as its database:
- Local development: Local PostgreSQL instance
- Production: PostgreSQL on AWS RDS
This approach allows for:
- Data persistence: Reliable storage of application data
- Development flexibility: Easy local development with PostgreSQL
Database Schema
The database schema is defined using Drizzle ORM in TypeScript. This provides:
- Type safety: Full TypeScript support for database operations
- Migration management: Easy schema updates and versioning
- Query building: Type-safe query builder
Environment Variables
The following environment variables are used for database configuration:
Local Development
POSTGRES_LOCAL_HOST
: The hostname of your PostgreSQL instance (typically 'localhost')POSTGRES_LOCAL_PORT
: The port for your PostgreSQL connection (typically 5432)POSTGRES_LOCAL_USER
: PostgreSQL usernamePOSTGRES_LOCAL_PASSWORD
: PostgreSQL passwordPOSTGRES_LOCAL_DB
: PostgreSQL database name
Production (AWS RDS)
USE_RDS
: Set to "true" to use AWS RDS PostgreSQLPOSTGRES_HOST
: The hostname of your PostgreSQL RDS instancePOSTGRES_PORT
: The port for your PostgreSQL connection (typically 5432)POSTGRES_USER
: PostgreSQL usernamePOSTGRES_PASSWORD
: PostgreSQL passwordPOSTGRES_DB
: PostgreSQL database namePOSTGRES_SSL
: Set to "true" to use SSL for the connectionCUSTOM_AWS_ACCESS_KEY_ID
: AWS access key IDCUSTOM_AWS_SECRET_ACCESS_KEY
: AWS secret access keySECURITY_GROUP_ID
: The security group attached to AWS RDS PostgreSQL
You can retrieve the value for these environment variables from AWS Amplify console's "Secrets" page for the hosted-components
project.
Make sure your public local IP is added as an inbound rule to the security group attached to the RDS.
Testing Production Locally
If testing production environment locally, you'll also need:
NODE_ENV
: Set to "production"
Local Development
For local development:
-
Install PostgreSQL locally
-
Set up your local database:
# Create the database createdb hosted_components # Apply migrations npm run db:migrate
-
No special environment variables are needed for local development as the application will connect to your local PostgreSQL instance by default. If needed to change the default local configuration though, change
POSTGRES_LOCAL_...
etc in the environment variables.
Any changes you make will update your local PostgreSQL database.
Production Setup
In production:
- Set up a PostgreSQL database on AWS RDS
- Configure environment variables:
USE_RDS=true POSTGRES_HOST=your-rds-instance.region.rds.amazonaws.com POSTGRES_PORT=5432 POSTGRES_USER=your_user POSTGRES_PASSWORD=your_password POSTGRES_DB=your_database
Viewing the Database
To view your local PostgreSQL database:
-
Using Drizzle Studio:
npm run db:studio
-
Using PostgreSQL tools:
Schema Changes and Migrations
To make schema changes and create migrations:
-
Modify the schema:
- Update the
schema-pg.ts
file with your schema changes
- Update the
-
Generate migrations:
npm run db:generate -- --name=add-css-column-to-styling
-
Apply migrations:
npm run db:migrate
Troubleshooting
Common issues and solutions:
-
Connection issues:
- Check your DATABASE_URL
- Verify PostgreSQL is running
- Check network/firewall settings
-
Migration failures:
- Ensure migrations are in correct order
- Check for conflicting changes
- Review migration logs