/replit-tutorials

How to use a database with Replit

Learn how to set up, connect, and manage a database in Replit with this simple guide for building dynamic, data‑driven apps.

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 use a database with Replit

The simplest, most reliable way to use a database with Replit is to rely on an external managed database (like PostgreSQL or MongoDB from a cloud provider) and connect to it from your Replit project using a connection string stored in Replit Secrets. Replit’s built‑in “Replit Database” works fine for very small prototypes, but real projects should use a proper external DB because it’s persistent, stable, and not tied to a single Repl’s lifecycle.

 

Two Real Options You Can Use

 

On Replit today, you realistically have two valid ways to use a database:

  • Replit Database — a tiny key/value store built into Replit. Great for quick demos or teaching. Not suited for production, relational data, multiple collections, or heavy writes.
  • External Managed Database (PostgreSQL, MySQL, MongoDB, Redis, etc.) — best for any real app. Works like local development but you store the connection string in Replit Secrets so it’s safe.

 

When to Use Each

 

  • Use Replit Database if you’re building a toy app, a school project, or just need to store a few simple key/value pairs (strings only).
  • Use an External DB for anything involving user accounts, sessions, authentication, analytics, relational models, or anything that must be stable long‑term.

 

Using Replit Database (Simple Option)

 

Replit DB is a key/value store. Everything you put into it becomes a string, so if you want to store objects, you convert them to JSON.

Example (Node.js):

import Database from "@replit/database";
const db = new Database();

async function run() {
  // Save data
  await db.set("user1", JSON.stringify({ name: "Alice", age: 23 }));
  
  // Read data
  const raw = await db.get("user1");
  const user = JSON.parse(raw);

  console.log(user); 
}

run(); // Run the function

Limitations you need to know:

  • It’s not SQL or NoSQL — it’s just simple key/value pairs.
  • No real querying — you can’t search by fields; you must fetch whole keys.
  • Not built for heavy load.
  • Not ideal for multi-user apps or anything long-term.

If any of these limitations matter, skip Replit DB and go straight to an external DB.

 

Using an External Database (Recommended for Real Projects)

 

This is how most serious Replit devs work. You create a cloud database (PostgreSQL, MongoDB, etc.), get a connection string, put that string in Replit Secrets, and connect to it from your app.

Why this is better:

  • Your data persists even if you delete the Repl.
  • You get backups, real queries, indexes, migrations.
  • No strange Replit DB limits.

 

Step-by-Step Setup

 

  • Create a database on a provider you like (e.g., Supabase or Railway for PostgreSQL, MongoDB Atlas for MongoDB).
  • Copy the database’s connection string.
  • In Replit, open the left sidebar → Secrets → create a key like DATABASE\_URL and paste the connection string.
  • Use that environment variable in your code.

 

Example: Node.js + PostgreSQL

 

import pg from "pg";

const client = new pg.Client({
  connectionString: process.env.DATABASE_URL
});

async function start() {
  await client.connect(); // Connect to the external PostgreSQL database

  await client.query(`
    CREATE TABLE IF NOT EXISTS users (
      id SERIAL PRIMARY KEY,
      name TEXT NOT NULL
    );
  `);

  await client.query("INSERT INTO users (name) VALUES ($1)", ["Alice"]);
  
  const result = await client.query("SELECT * FROM users");
  console.log(result.rows); // See your data
}

start();

This code works exactly like a normal server project on your machine — except your secrets stay safe inside Replit’s Secrets Manager.

 

Important Replit-Specific Tips

 

  • Never hardcode passwords or connection strings inside code. Always use Secrets.
  • Do not rely on the filesystem to store data; Replit’s filesystem is not meant for databases.
  • If you're using Replit Deployments, Secrets still work the same, but make sure your database allows external connections.
  • Keep your DB open to only specific IPs if your provider supports allowlists (you may need to allow 0.0.0.0/0 for hobby use, which is normal when learning).

 

Summary

 

If you're building anything real on Replit, use an external managed database and store your connection string in Secrets. Replit Database is fine for experiments but not for serious work. External DBs behave predictably, scale well, and match how professional apps work outside Replit.

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