/replit-tutorials

How to connect Replit to external databases

Learn how to connect Replit to external databases with simple steps, setup tips, and best practices for secure, reliable project integration.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to connect Replit to external databases

To connect Replit to an external database, you simply install the correct client library for that database, add your database credentials into Replit Secrets (so they’re not exposed publicly), and then use the database’s standard connection URL inside your app. Replit doesn’t block outbound connections, so as long as your database allows external access and your IP rules are set correctly, it works like any other cloud environment.

 

Why this works (short version)

 

Replit containers can make normal outgoing network requests. So any external database that provides a public connection string (Postgres, MySQL, MongoDB Atlas, Redis services, etc.) can be reached from inside your Repl. The only things you must handle are: storing credentials safely, making sure your database accepts connections from Replit, and avoiding hardcoding secrets.

 

How to do it step by step

 

Here’s the exact, real way developers connect external databases to Replit without breaking things:

  • Create or obtain your database. This might be a hosted Postgres (Supabase, Neon, Railway), MongoDB Atlas cluster, a MySQL instance, or anything else that accepts a connection string.
  • Find the connection URL. This usually looks like:
    Postgres: postgres://user:password@host:5432/dbname
    MongoDB: mongodb+srv://user:[email protected]/dbname
  • Put that URL inside Replit Secrets. Go to Replit sidebar → Secrets (key icon) → Add:
    Key: DATABASE\_URL
    Value: your full connection string
    This hides your credentials and keeps them out of version control.
  • Install the correct database client library for your language.
  • Use the secret in your code — access it via environment variables.
  • Verify your database firewall rules. For providers like MongoDB Atlas or some Postgres hosts, you must allow access from “0.0.0.0/0” (or a restricted range if the provider supports dynamic IPs). Replit uses dynamic outbound IPs so you usually cannot whitelist a single IP.

 

Concrete, working examples

 

Below are real examples that work inside Replit today.

 

Node.js → Postgres (Supabase, Neon, Railway, etc.)

 

// index.js
import pkg from 'pg'          // pg is the official Postgres client
const { Client } = pkg

// Replit stores secrets in environment variables
const client = new Client({
  connectionString: process.env.DATABASE_URL
})

async function start() {
  await client.connect()      // Connect to your external DB

  const result = await client.query('SELECT NOW()') // Test query
  console.log(result.rows)

  await client.end()
}

start()

 

Install the package in Replit shell:

npm install pg

 

Node.js → MongoDB Atlas

 

// index.js
import { MongoClient } from 'mongodb'

const uri = process.env.MONGO_URL   // Stored in Replit Secrets
const client = new MongoClient(uri)

async function run() {
  await client.connect()            // Establish Atlas connection

  const db = client.db('test')
  const col = db.collection('users')

  const user = await col.findOne({})
  console.log(user)

  await client.close()
}

run()

 

Install MongoDB client:

npm install mongodb

 

Python → Postgres

 

# main.py
import os
import psycopg2

conn = psycopg2.connect(os.getenv("DATABASE_URL"))
cur = conn.cursor()

cur.execute("SELECT NOW();")
print(cur.fetchone())

cur.close()
conn.close()

 

Install:

pip install psycopg2-binary

 

Common pitfalls in Replit

 

  • Firewall/whitelisting issues. Many database providers require you to whitelist IPs. Replit has changing outbound IPs, so you must allow public access (0.0.0.0/0) or use a provider that doesn’t enforce strict IP rules.
  • Hardcoding credentials. Never paste passwords directly in code or config files. Everything goes into Secrets.
  • Long-running connections during Replit sleep. Free Repl containers go to sleep; if you're using a long-open DB connection, reconnect on startup.
  • Container restarts. Always handle reconnect logic because Replit can restart your container if you're on a free plan or after inactivity.

 

How you know the connection is successful

 

  • Your test query returns a row instead of hanging or throwing a timeout.
  • You see logs from your DB provider showing an inbound connection from Replit.

 

If you follow this flow — secrets in Replit, correct client library, correct firewall settings, and a simple test query — connecting Replit to any external database is straightforward and reliable for real-world projects.

Still stuck?
Copy this prompt into ChatGPT and get a clear, personalized explanation.

This prompt helps an AI assistant understand your setup and guide you through the fix step by step, without assuming technical knowledge.

AI AI Prompt

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022