Prerequisites
- PostgreSQL installed (see Environment Setup)
- Environment variables configured
- Node.js and npm installed
PostgreSQL Installation and Configuration
Local PostgreSQL Setup
Using Supabase Database
Supabase provides a managed PostgreSQL database. If you’re using Supabase for both authentication and database, you can use the database credentials from your Supabase project.
Get database credentials
In your Supabase dashboard:
- Go to Project Settings > Database
- Find the “Connection string” section
- Use the “URI” or individual connection parameters
Database Creation
If you’re using Supabase, the database is already created. You’ll connect to the default
postgres database or create a new one through the Supabase SQL Editor.Database Schema and Migrations
Automatic Schema Synchronization
The POS Nest API uses TypeORM withsynchronize: true enabled in development:
Schema Creation on First Run
Ensure database exists and is accessible
Verify your
.env file has the correct database credentials.Start the application
- Connect to your PostgreSQL database
- Scan for all
*.entity.tsfiles - Create tables based on your entity definitions
- Create relationships and indexes
Production Migrations
For production deployments, disablesynchronize and use TypeORM migrations:
You’ll need to add these scripts to
package.json if you plan to use migrations:Seeding Data
The application includes a seeder to populate your database with initial data.Running the Seeder
Ensure database is set up
Make sure your database exists and the application has created the schema (run
npm run start:dev at least once).Run the seed command
src/seeder.ts which:- Creates a NestJS application context with the
SeederModule - Runs the
SeederService.seed()method - Populates the database with initial data
- Closes the application context
The seeder is idempotent-safe depending on its implementation. Check
src/seeder/seeder.service.ts to see if it clears existing data or skips already-seeded records.Database Connection Testing
Test Connection on Application Start
Watch for connection messages
You should see logs indicating successful database connection. If there are connection errors, they’ll appear in the console.
Manual Connection Test
Test your database connection directly:Troubleshooting Connection Issues
Connection refused
Connection refused
- Verify PostgreSQL is running:
sudo systemctl status postgresql(Linux) orbrew services list(macOS) - Check if PostgreSQL is listening on the correct port:
sudo netstat -plnt | grep 5432 - Ensure firewall isn’t blocking port 5432
Authentication failed
Authentication failed
- Verify username and password in
.envare correct - Check PostgreSQL’s
pg_hba.confallows password authentication - Try connecting with
psqldirectly to confirm credentials
Database does not exist
Database does not exist
- Verify database name in
.envmatches the created database - Create the database if it doesn’t exist:
CREATE DATABASE pos_nest_db; - Check for typos in
DATABASE_NAMEenvironment variable
SSL connection errors
SSL connection errors
The application is configured with
ssl: { rejectUnauthorized: false }. If you need strict SSL:Database Configuration Reference
The database configuration is defined insrc/config/typeorm.config.ts:
Configuration Options
| Option | Description | Development | Production |
|---|---|---|---|
synchronize | Auto-create tables from entities | true | false |
logging | Enable SQL query logging | false or true for debugging | false |
ssl | SSL connection settings | Optional | Recommended |
entities | Path to entity files | Auto-discovered | Auto-discovered |
Next Steps
Deployment
Learn how to deploy your API to production
API Reference
Start using the API endpoints
