Initial Database Setup
For a fresh database installation, use the automated setup script:- Applies the initial schema from
src/db/migrations/0000_initial_schema.sql - Creates all tables (users, properties, contacts, etc.)
- Seeds initial data (property types, characteristics, admin user)
Drizzle Migration Commands
The API uses Drizzle ORM for database migrations. Available commands:Generate Migrations
Create migration files from schema changes:src/db/migrations/.
Apply Migrations
Run pending migrations against your database:Push Schema Changes
Seed Database
Populate the database with initial data:- Admin user account
- Property types (house, apartment, land, commercial)
- Property characteristics (bedrooms, bathrooms, parking)
- Other metadata
Run Custom Migrations
Execute specific migration scripts:Production Migration with Data Preservation
When migrating a production database with existing property listings, follow these steps to preserve data.Prerequisites
- Access to your VPS terminal
pg_dumpandpsqltools installed- Your production
DATABASE_URLenvironment variable
Backup Property Listings
Export existing property data to a file using This creates a SQL file containing INSERT statements for all properties.
--data-only to avoid schema conflicts:Seed Initial Data
Populate the database with metadata and admin user:This creates:
- Admin user account
- Property types and characteristics
- Default metadata
Migration Files
The API includes these migration files insrc/db/migrations/:
0000_initial_schema.sql- Initial database schema0001_add_garage_spaces.sql- Adds garage spaces column0003_add_youtube_video_url.sql- Adds YouTube video URL field
npm run db:migrate.
Database Connection
Ensure yourDATABASE_URL environment variable is set:
For production databases, always use SSL (
sslmode=require) to encrypt database connections.Troubleshooting
Connection Issues
If you encounter connection errors:- Verify
DATABASE_URLis correctly set - Check that your database accepts connections from your server IP
- Ensure SSL is properly configured
- Test connection with
psqldirectly
Migration Failures
If migrations fail:- Check database logs for specific errors
- Verify you have sufficient database permissions
- Ensure no conflicting schema changes exist
- Review migration files in
src/db/migrations/
Data Loss Prevention
- Always backup before migrations:
pg_dump -Fc database > backup.dump - Test migrations in staging environment first
- Use
--data-onlyflag when backing up for schema changes - Keep backup files until migration is verified successful