Phones and mobile apps2 minutes to read

Should my mobile app connect directly to my database?

No. Credentials inside an app can be extracted, so a MySQL, PostgreSQL or MongoDB server should sit behind a small API that checks every request.

No. Put a small API in between

No. A database password or certificate shipped inside an app can be extracted from the download, and then anyone can read or delete your data. Phones also change IP address constantly, so you can’t restrict access by address. Have the app call an HTTPS API that you host. The API checks who is asking, runs only the queries you wrote, and is the only thing that holds the database credentials.

How the pieces connect

Request path
StepHoldsChecks
Phone appThe API URL and the user’s session tokenNothing secret
Your APIDatabase credentials in VariablesSession, input size, which rows this user may touch
DatabaseYour dataA client certificate and an allowed address list

On Bothosting, managed databases accept connections only with a client certificate, and the Connection page can restrict which client IP addresses may connect. That suits a server, which has one stable address and a private place to keep the certificate. It doesn’t suit an app installed on thousands of phones. Connecting to a managed database.

Write one route per thing the app does

A route that only returns the signed-in user’s notes
app.get('/v1/notes', requireUser, async (req, res) => {
  const { rows } = await db.query(
    'SELECT id, title, updated_at FROM notes WHERE owner_id = $1 ORDER BY updated_at DESC LIMIT 50',
    [req.user.id],
  );
  res.json(rows);
});

The query takes the user ID from the verified session, never from the request body. It uses a placeholder instead of string building, and it caps the number of rows. A route like this can’t be used to read another user’s notes, even by someone who copies the app’s traffic.

The exception: databases built for direct access

Firebase and Supabase are designed to be called straight from an app. They rely on security rules or row-level security that you write for every table. If you use one, those rules play the role of the API, so test them as carefully as you would test code. A standard MySQL, PostgreSQL or MongoDB server has no such layer for app users and belongs behind an API. Hosting a backend for a mobile app.