I've lost more afternoons to "just connect the database" than I care to admit. It always starts optimistic. Then you're three hours deep, staring at a timeout, wondering if PostgreSQL has a personal vendetta against you.
Connecting a Replit app to AWS RDS PostgreSQL is simple in the same way "configure DNS" is simple. The steps are short. The ways to mess it up are everywhere.
The three things that must be true
If any of these are wrong, you're not "kind of connected." You're dead in the water.
- You have the right RDS endpoint (host)
- Your RDS security group allows inbound traffic (network)
- Your connection string is valid (auth + format)
Step 1: Get the RDS connection details (don't guess)
In AWS, open your RDS instance. Copy the endpoint. Don't type it by hand - you'll typo it and waste 20 minutes.
It looks like:
your-db-name.abc123xyz.us-east-1.rds.amazonaws.com
You also need:
- Port:
5432(default) - Database name: whatever you created (or
postgresif you didn't) - Username + password: the master credentials (case-sensitive)
Step 2: Make RDS publicly accessible (yes, really)
If you're connecting from Replit, your app is not inside your AWS VPC. So your RDS instance needs:
- Public accessibility = Yes
This feels wrong because it is wrong-ish. But it's how most people start.
If you leave it as "No," you'll get timeouts and you'll start blaming libraries. Don't. It's networking.
Step 3: Security group rules (most timeouts live here)
90% of "it hangs forever" is your security group blocking inbound traffic.
Add an inbound rule to the RDS security group:
- Type: PostgreSQL
- Port: 5432
- Source:
0.0.0.0/0
Yes, that's the entire internet.
Replit also doesn't give you a stable outbound IP by default. So you can't cleanly whitelist your server unless you're on a plan that supports it (Reserved VM / Dedicated VM). If you can get a static outbound IP, do it. Then change the security group source from 0.0.0.0/0 to that single IP.
Don't leave 0.0.0.0/0 in production. I know you will anyway. But don't.
Also: your database is now credential-protected and exposed. Use a strong password. Don't reuse one from your "test123" era.
Step 4: Put the connection string in Replit Secrets
Never hardcode credentials in your repo. Put them in Replit's Secrets (Tools panel).
Set:
DATABASE_URL
Format:
postgresql://[user]:[password]@[host]:[port]/[database]
If your password has special characters (like @ or :), URL-encode it or you'll get "authentication failed" while you swear the password is correct. Because it is correct. And also broken.
Step 5: Code examples that actually work
Python + psycopg2
import os
import psycopg2
DATABASE_URL = os.environ.get("DATABASE_URL")
conn = psycopg2.connect(DATABASE_URL)
Python + SQLAlchemy
import os
from sqlalchemy import create_engine
engine = create_engine(os.environ.get("DATABASE_URL"))
TypeScript + pg (client)
import { Client } from 'pg';
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
});
await client.connect();
TypeScript + pg (pool, better for real apps)
import { Pool } from 'pg';
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
});
SSL: the other "why is this failing" moment
RDS uses SSL. Some clients will complain about cert validation. The pragmatic fix is:
ssl: { rejectUnauthorized: false }
Less secure, but it gets you moving. Another option is adding:
?sslmode=require
to the end of your connection string.
Troubleshooting: match the symptom to the cause
- Timeout / hangs: security group inbound rule is wrong (or missing)
- "Could not connect to server": public accessibility is off, endpoint typo, wrong port
- "Authentication failed": wrong username/password, URL encoding issue
- SSL errors: add
?sslmode=requireor loosen SSL validation in the client
One more real-world warning: RDS costs money while you sleep. I've seen startups burn $500/month on RDS instances they forgot about. Set a reminder. Delete what you're not using.
If you're stuck, check the security group first. 90% of the time, that's the problem. Even if you're sure you already fixed it.
-Sethers