-
Install PostgreSQL locally
# macOS brew install postgresql # Ubuntu/Debian sudo apt-get install postgresql postgresql-contrib # Windows - Download from https://www.postgresql.org/download/windows/
-
Create database
createdb k8s
-
Configure
.env.localDATABASE_URL="postgresql://postgres:ayush@2004@localhost:5432/k8s" -
Start development server
npm run dev
-
Create RDS PostgreSQL instance
- Engine: PostgreSQL 14+
- Instance: db.t3.micro (free tier eligible)
- Storage: 20GB
- Backup retention: 7 days
-
Configure security group
- Inbound rules: Allow port 5432 from your app's IP
-
Update
.env.localfor productionDATABASE_URL="postgresql://admin:your_strong_password@mini-kub.xxxxxxxxxxxx.us-east-1.rds.amazonaws.com:5432/k8s?sslmode=require"
-
Connect GitHub repository
git push origin main
-
Create PostgreSQL service in Railway
- Automatically provides
DATABASE_URL
- Automatically provides
-
Deploy Next.js app
- Railway automatically reads
.env.local
- Railway automatically reads
- Create Neon account → https://neon.tech
- Get connection string → Automatically set as DATABASE_URL
- Deploy on Vercel
npm install -g vercel vercel
Create docker-compose.yml:
version: '3.8'
services:
postgres:
image: postgres:15
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ayush@2004
POSTGRES_DB: k8s
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
app:
build: .
ports:
- "3000:3000"
environment:
DATABASE_URL: "postgresql://postgres:ayush@2004@postgres:5432/k8s"
depends_on:
- postgres
command: npm run dev
volumes:
postgres_data:Run:
docker-compose upDATABASE_URL- PostgreSQL connection string
NODE_ENV- Set to "production" for optimal performanceNEXT_PUBLIC_API_URL- API base URL (defaults to current origin)
- Database is accessible from your deployment environment
- Connection string is set in environment variables
- SSL mode is configured for remote databases (
?sslmode=require) - Database user has minimal required permissions
- Firewall/Security groups allow app to reach database
-
.env.localis in.gitignore(NEVER commit secrets!) - Test connection before full deployment
- Never commit
.env.local→ Already in.gitignore - Use strong passwords for database users
- Enable SSL for remote databases
- Restrict database access with security groups/firewall
- Use read-only replicas for analytics (if needed)
- Enable automated backups for production
- Check database is running
- Verify firewall allows port 5432
- Confirm correct hostname/IP address
- Check security group rules (AWS RDS)
- Verify username and password
- Check database name exists
- Ensure user has correct privileges
- For local: Remove
?sslmode=require - For AWS RDS: Keep
?sslmode=require - Certificate issues: Use
?sslmode=prefer
- Choose your deployment platform
- Create a PostgreSQL database
- Get the connection string
- Add to
.env.local - Test locally:
npm run dev - Deploy to your platform
For questions, refer to:
- PostgreSQL docs: https://www.postgresql.org/docs/
- Next.js deployment: https://nextjs.org/docs/deployment