Guides / Prompting and shipping

How to Add a Database to Your App (Without Losing Data Later)

How to add a database to an app you built: when you need one, Postgres vs hosted options, designing tables, prompts to use, and mistakes that lose data.

Mythex Team · 2026-09-29 · 6 min read

To add a database to your app, pick a managed relational database (Postgres is the usual default), keep its connection string in a server-side secret, create one table per kind of thing your app stores, and make your backend — never the browser — read and write to it. If you build with an AI app builder, you can usually just say "save this to a database" and the agent sets up the tables and code; your job is to describe the data clearly and check it actually persists.

This guide covers when you need a database, the main options, how to describe your data, and the mistakes that cause lost or leaked data.

When your app needs a database

You need a database when your app has to remember something after the page is closed, or share data between people. Typical signs:

  • Visitors submit something: a form, a sign-up, a booking, an order
  • Users have accounts with their own saved content
  • An admin needs to see or edit what others created
  • You want reports or counts over time

You don't need one for a static site where everyone sees the same content, like a portfolio or a simple landing page. If you're unsure what a database actually is, read what is a database first.

Your options

OptionWhat it isGood forTrade-offs
Built-in database from your app builderA database created and connected for your projectMost apps built with an AI builderTied to that platform's hosting; export and backups depend on the platform
Managed Postgres or MySQLA hosted relational database you sign up for separatelyApps you host yourself, or that several services shareYou manage keys, connection limits and plans
Backend-as-a-serviceDatabase plus auth, storage and APIs in one productFrontend-heavy apps that want ready-made loginMore vendor-specific concepts to learn
Document (NoSQL) databaseStores flexible JSON-like documentsVery irregular dataRelationships and reporting are harder
A spreadsheet as a databaseGoogle Sheets or similar via an APITiny internal tools, prototypesSlow, no real constraints, breaks under concurrent edits

For most small and medium web apps, a relational database like Postgres is the right default. It is good at relationships (a customer has many orders), keeps data consistent, and lets you answer questions later with SQL. See what is Postgres for a plain-English overview.

Describe your data before you build

The most useful thing you can do is write down the things your app stores and how they connect. You don't need database jargon. For a booking app:

  • Customers: name, email, phone
  • Services: name, duration, price
  • Bookings: which customer, which service, start time, status (pending, confirmed, cancelled)

That's three tables. "Which customer" and "which service" become links (foreign keys) between them. Getting this right early matters far more than any technology choice, because changing the shape of data that already has real rows in it is harder than changing a page layout.

A few rules of thumb:

  • One table per kind of thing. Don't pack orders into a text column on the customer.
  • Give every row an ID and a created-at time. You'll want both later.
  • Use proper types. Dates as dates, money as whole cents or a decimal type, not as text.
  • Mark required fields as required. Let the database reject bad data, not just the form.

Example prompts

Start with one feature at a time rather than dumping a whole schema:

Add a database to this app. Create a bookings table with customer name, email, service, start time and status (pending, confirmed, cancelled; default pending). Save submissions from the booking form to it, show a success message, and add an admin page that lists bookings newest first.

Then extend it:

Add a services table (name, duration in minutes, price in cents). Replace the free-text service field on bookings with a link to services, and migrate existing bookings without deleting them.

And tighten it:

Make sure all database reads and writes happen on the server. The browser must never receive the database connection string. Validate the email and start time on the server before inserting.

Step by step

  1. List your data as above: things, fields, links between them.
  2. Create the database. In an AI builder, ask for it in chat. Elsewhere, create a managed Postgres instance and copy its connection string.
  3. Store the connection string as a secret (an environment variable on the server), never in code. See environment variables and secrets.
  4. Create the tables. Use migrations — versioned scripts that change the schema — so changes are repeatable and reversible.
  5. Build the server routes that create, read, update and delete rows (CRUD). The browser calls these routes; it never talks to the database directly.
  6. Wire the UI to those routes: forms that save, lists that load, empty states when there's nothing yet.
  7. Test persistence. Submit something, reload the page, open it in a different browser. If the data isn't there, it wasn't saved to the database.
  8. Check access. Log in as a second user and confirm you can't see or edit the first user's data.
  9. Publish, then test again on the live URL, since the published app may use a different connection than your preview.

Common mistakes

  • Saving to the browser and thinking it's saved. localStorage looks like it works until someone opens the app on another device. Always test in a second browser.
  • Putting the connection string in frontend code. Anyone can read frontend code. That gives them your whole database.
  • No access checks. "Show my orders" must filter by the logged-in user on the server, not in the UI. This is the most common security hole in AI-built apps; our security checklist covers it.
  • Destructive schema changes. Asking to "rename the table" or "change the structure" can drop and recreate it. When real data exists, say "migrate the existing rows, don't delete anything".
  • Storing files in the database. Images and PDFs belong in file storage, with only their URL or key in a table. See how to add file uploads.
  • No backups. Know how your platform backs up data and how you'd restore it before you have customers.

Checklist

  • Every kind of thing has its own table with an ID and created-at time
  • Required fields and types are enforced by the database
  • The connection string lives only in server-side secrets
  • All reads and writes go through server routes
  • Each user can only read and change their own rows
  • Data survives a reload and shows up in a different browser
  • Schema changes use migrations and keep existing rows
  • You know how backups and restores work
  • The live app has been tested after publishing

Adding a database in Mythex

On Mythex, each project can get its own dedicated Postgres database, on both Free and Pro. You usually don't click a separate "attach" button: ask in chat ("save waitlist emails to a database") or type /database, and the connection details are injected into the workspace and your published app. Project settings shows whether a database is active.

The database sleeps after 5 idle minutes and wakes on the next request. Published usage is charged for the time it was actually awake, from the same credit balance you use for building. If you add a database after you've already published, publish again so the live app picks it up.

See Database and the Build a CRUD app recipe in the docs. For the login side of per-user data, read how to add login to your app.

Questions

Do I need a database for my app?

You need one as soon as your app has to remember something between visits or share data between users: sign-ups, orders, bookings, posts or settings. A static website that only shows the same content to everyone does not need one.

Which database should I use for a small web app?

A relational database such as Postgres is a safe default for most small web apps. It handles users, orders, relationships and reporting well, is widely supported, and you rarely outgrow it at small scale.

Can I store data in the browser instead of a database?

Browser storage such as localStorage only lives on one device, in one browser, and the user can clear it at any time. It is fine for preferences like a theme choice, but not for anything you or other users need to see later.

Does Mythex include a database?

Yes. A Mythex project can get its own dedicated Postgres database on Free and Pro. You ask for it in chat or use the /database command, and the connection details are injected for you. Usage is paid from the same credit balance as building.

Keep reading

  • How to Publish an App on Google Play: The Real Play Console Steps — How to publish an app on Google Play: developer account, Create app, store listing, App content forms, the 12-tester closed test and production release.
  • How to Publish an App on the App Store: TestFlight to Approval — How to publish an app on the App Store: Developer Program, App Store Connect, TestFlight, listing, App Privacy, submitting for review and rejections.
  • How to Use Google AI Studio: Playground, Build Mode and Vibe Coding With Gemini — How to use Google AI Studio: test Gemini in the Playground, get an API key, vibe code an app in Build mode, and know when to move to a full-stack builder.
  • Is Vibe Coding Safe? The Real Risks and What to Check Before You Publish — Is vibe coding safe? It can be. The real risks (data leaks, exposed keys, abuse), how to keep secrets safe, and a 30-minute check before you publish.
  • Vibe Coding for Beginners: How to Start, Step by Step — A vibe coding tutorial for complete beginners: pick a tool, write a first prompt, test, fix and publish a small app in one afternoon. No coding needed.
  • 30 Vibe Coding Prompts You Can Copy (Cheat Sheet) — 30 vibe coding prompts you can copy and paste, grouped by stage: first build, planning, design, data, fixing bugs, testing, security and publishing.

Start building free · Templates · Docs

አማርኛ العربية Български বাংলা Bosanski Català Čeština Dansk Deutsch Ελληνικά Español Español (España) Eesti فارسی Suomi Français Français (Canada) ગુજરાતી हिन्दी Hrvatski Magyar Հայերեն Bahasa Indonesia Íslenska Italiano 日本語 ქართული Қазақ тілі ಕನ್ನಡ 한국어 Lietuvių Latviešu Македонски മലയാളം Монгол मराठी Bahasa Melayu မြန်မာ Norsk bokmål Nederlands ਪੰਜਾਬੀ Polski Português (Brasil) Português (Portugal) Română Русский Slovenčina Slovenščina Soomaali Shqip Srpski Svenska Kiswahili தமிழ் తెలుగు ไทย Tagalog Türkçe Українська اردو Tiếng Việt 简体中文 繁體中文(香港) 繁體中文(台灣)