This guide covers setting up PostgreSQL database for your application.
Recommended Options:
After creating your database, you’ll get a connection string like:
postgresql://username:password@host:port/database
Create .env.local file:
DATABASE_URL=postgresql://username:password@host:port/database
JWT_SECRET=your-secret-key-here
npm run db:setup
This will:
Stores admin user accounts for the admin panel.
CREATE TABLE admin_users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
role VARCHAR(20) DEFAULT 'admin',
is_active BOOLEAN DEFAULT true,
last_login TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Stores contact form submissions and inquiries.
CREATE TABLE inquiries (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(50),
company VARCHAR(255),
service VARCHAR(100) NOT NULL,
budget VARCHAR(50),
timeline VARCHAR(50),
message TEXT NOT NULL,
status VARCHAR(20) DEFAULT 'pending',
priority VARCHAR(20) DEFAULT 'normal',
assigned_to UUID REFERENCES admin_users(id),
notes TEXT,
ip_address VARCHAR(45),
user_agent TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
replied_at TIMESTAMP
);
Stores newsletter subscription data.
CREATE TABLE newsletter_subscribers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(255),
status VARCHAR(20) DEFAULT 'active',
source VARCHAR(50),
subscribed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
unsubscribed_at TIMESTAMP
);
Stores blog articles and content.
CREATE TABLE blog_posts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR(255) NOT NULL,
slug VARCHAR(255) UNIQUE NOT NULL,
excerpt TEXT,
content TEXT NOT NULL,
author_id UUID REFERENCES admin_users(id),
featured_image VARCHAR(500),
category VARCHAR(100),
tags TEXT[],
status VARCHAR(20) DEFAULT 'draft',
published_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
If you prefer manual setup or the script fails:
# PostgreSQL command line
createdb limitless_infotech
# Or using psql
psql -U postgres
CREATE DATABASE limitless_infotech;
psql -U postgres -d limitless_infotech -f lib/database/schema.sql
npm run db:create-admin
Or manually:
INSERT INTO admin_users (username, email, password_hash, role)
VALUES (
'admin',
'admin@limitlessinfotech.com',
'$2b$10$...', -- bcrypt hash of your password
'admin'
);
[YOUR-PASSWORD] with your database passwordlib/database/schema.sqlDATABASE_URL=postgresql://postgres:[password]@db.[project-ref].supabase.co:5432/postgres
DATABASE_URL=postgresql://postgres:password@containers-us-west-xxx.railway.app:7431/railway
DATABASE_URL=postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb
# Ubuntu/Debian
sudo apt update
sudo apt install postgresql postgresql-contrib
# macOS
brew install postgresql
brew services start postgresql
# Windows
# Download from postgresql.org
sudo -u postgres psql
CREATE DATABASE limitless_infotech;
CREATE USER limitless_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE limitless_infotech TO limitless_user;
DATABASE_URL=postgresql://limitless_user:your_password@localhost:5432/limitless_infotech
# Generate secure password
openssl rand -base64 32
DATABASE_URL=postgresql://user:pass@host:5432/db?sslmode=require
// Already configured in lib/database/connection.ts
max: 20, // Maximum connections
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000
.env.local to git# Automated backup script
pg_dump $DATABASE_URL > backup_$(date +%Y%m%d).sql
# Restore from backup
psql $DATABASE_URL < backup_20241128.sql
# Create test file
cat > test-db.js << 'EOF'
require('dotenv').config({ path: '.env.local' })
const { testConnection } = require('./lib/database/connection')
testConnection()
.then(success => {
if (success) {
console.log('✅ Database connection successful!')
process.exit(0)
} else {
console.error('❌ Database connection failed!')
process.exit(1)
}
})
EOF
# Run test
node test-db.js
# Using psql
psql $DATABASE_URL -c "SELECT NOW();"
# Using Node.js
node -e "
const { Pool } = require('pg');
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
pool.query('SELECT NOW()', (err, res) => {
console.log(err ? err : res.rows[0]);
pool.end();
});
"
VACUUM ANALYZE;
SELECT
schemaname,
tablename,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size
FROM pg_tables
WHERE schemaname = 'public'
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
SELECT count(*) FROM pg_stat_activity;
SELECT query, calls, total_time, mean_time
FROM pg_stat_statements
ORDER BY mean_time DESC
LIMIT 10;
# Check if PostgreSQL is running
sudo systemctl status postgresql
# Check port
sudo netstat -plnt | grep 5432
# Check firewall
sudo ufw status
pg_hba.conf settings# Disable SSL for local development
DATABASE_URL=postgresql://user:pass@localhost:5432/db?sslmode=disable
# Require SSL for production
DATABASE_URL=postgresql://user:pass@host:5432/db?sslmode=require
// Reduce pool size in connection.ts
max: 10, // Instead of 20
Need Help?