I've connected Replit to databases a dozen different ways. Here's the path that doesn't make you want to throw your laptop out the window.
I love Replit for quick prototypes and side projects. It's fast, it's easy, and it's dangerously good at convincing you your app is "basically production-ready." But when scalability comes into play, you'll find yourself cranking up the database server dial for a hefty fee.
If you want a database that won't break the bank nearly as bad, Google Cloud SQL for PostgreSQL is a solid upgrade.
Also worth noting: as of December 2025, Replit runs on Google Cloud. So your Replit app and your Cloud SQL instance can be neighbors in the same giant building full of computers and regret. This often means less latency and less weird billing.
Multi-cloud is fun until your bill arrives and you realize you've been paying AWS to talk to GCP to talk to your laptop.
Before we start (so you don't get stuck 20 minutes in)
Make sure you have:
- A GCP project with billing enabled
- A Cloud SQL (PostgreSQL) instance (or permission to create one)
- A Replit project where you can run your app
- A plan for how you'll connect (public IP or proxy)
Helpful docs (bookmark these for later when something times out):
- Cloud SQL PostgreSQL overview
- Cloud SQL Auth Proxy
Step 1: Create the database and user (the "boring but necessary" part)
Head over to the GCP Console and create a Cloud SQL PostgreSQL instance. Then create a database - I usually call mine something boring like appdb. Create a user (like appuser) and give it a password you'll immediately forget and have to reset later.
What you'll want to copy somewhere safe:
- Instance connection name (looks like
myproj:us-central1:mydb) - Database name (
appdb) - Username (
appuser) - Password (good luck)
Step 2: Choose how you'll connect (two options, both real, one less annoying)
You've got two ways to do this:
Option A: Public IP + Authorized Networks
Quick to set up, but Replit's egress IPs can change and you'll spend your afternoon updating allowlists.
Use this if you're moving fast and can tolerate some occasional "why is the database refusing me today?" moments.
Option B: Cloud SQL Auth Proxy (recommended)
More secure and you don't have to mess with IP addresses every time Replit decides to change something.
If you can run the proxy inside your Replit environment, do it. It's one of those rare times where "the recommended way" is actually the way you want.
Step 3: Put credentials in Replit Secrets (do not hardcode, seriously)
Don't hardcode your database password. Seriously. Use Replit's Secrets feature.
Add these secrets:
DB_HOST- If using the proxy: usually
127.0.0.1 - If using public IP: your Cloud SQL instance public IP or DNS
DB_PORT=5432DB_NAME=appdbDB_USER=appuserDB_PASSWORD= your password
This is one of those habits that saves you later. I've watched teams rotate credentials after accidentally committing them. It's not a fun meeting.
Step 4: Connect in code (Python example, same pattern everywhere)
Here's what the connection code looks like. I'm using Python and psycopg, but the pattern is basically the same in any language: read environment variables, connect, run a simple test query.
import os
import psycopg
conn = psycopg.connect(
host=os.environ["DB_HOST"],
port=os.environ.get("DB_PORT", "5432"),
dbname=os.environ["DB_NAME"],
user=os.environ["DB_USER"],
password=os.environ["DB_PASSWORD"],
)
with conn.cursor() as cur:
cur.execute("select now();")
print(cur.fetchone())
If that prints a timestamp, you're in business.
If you're looking for similar guides for AWS databases, I've also written about connecting to AWS RDS PostgreSQL, AWS RDS MySQL, AWS RDS MariaDB, and AWS RDS SQL Server.
Step 5: Cost + latency sanity check (aka "why this matters")
Because Replit is already on GCP, hosting Postgres on GCP often means:
- Lower latency (fewer network hops)
- Simpler billing (one provider)
- Less "why is this slow today?" debugging
It's not guaranteed, but staying on one cloud provider is one of those rare times when the boring choice actually makes sense.
One quick lesson from painful experience: I've seen people save money by "mixing and matching" services across clouds... and then lose it all (and more) in egress costs and random networking issues. The spreadsheet looked great. Reality did not.
-Sethers