Rook 0.8.7 + Pawn 0.1.0 surpassed top-tier agents in Claude Code for security. read more

How to Secure Your Database Before Going Live

7 MIN READ
How to Secure Your Database Before Going Live

Secure your database before going live by deciding who can open a connection, what that account can do, and how every query treats user input. The database is where the customer list, the password hashes, and the invoices actually sit. An application bug can leak a row. An open port with a tutorial password can leak the whole server. Builders following a generated docker-compose.yml often publish port 5432 to the internet and leave the user as a superuser because that is how the sample got the app running on a laptop.

This guide covers exposure, least privilege, parameterized queries, row policies, and backups. It is written for a solo developer about to point a real domain at a managed Postgres, MySQL, or similar store. Application checks still matter. Read them next to API security for your endpoints and authentication versus authorization. The OWASP Database Security Cheat Sheet is the broader map.

What "secure the database" covers before launch

Securing the database before launch means four outcomes. Strangers cannot open a connection. The app user cannot drop the server or read unrelated schemas. User input cannot rewrite a query. You can restore yesterday's data if today's deploy destroys it. Confirm encryption at rest in the provider console, then test the firewall on the instance you will actually use.

Five controls to finish before the first real user

Ship these five controls in order, on the production instance, before the first real user. An exposed superuser makes careful queries irrelevant. Create the app user, the firewall rule, and the backup schedule before you import customer data. A local container with trust authentication will not show you how the hosted firewall behaves.

Control What you configure How you know it worked
Network exposure Private network, allowlist, or provider firewall A connection from a phone hotspot fails
Least privilege App user with DML on its schema only The app user cannot create roles or read other databases
Parameterized queries Bound parameters in every query A quote in an input does not change the statement
Row checks or policies Owner column in queries, or row security User A cannot select user B's rows
Backups Automated backups plus one tested restore A scratch instance serves a known row

Managed databases still need an allowlist. Look for public access, allowed IPs, or authorized networks. Allow the application hosts. Deny 0.0.0.0/0. "It is in the cloud" still leaves that page for you to set.

Close the network

Put the database on a private network and let only the app servers connect. If you must reach it from a laptop, use the provider's proxy or a VPN, and turn that access off when you are done. Change the sample password, require TLS when the provider offers it, and confirm that a connection from an outside network is refused.

Grant least privilege

Create an application user that can select, insert, update, and delete on its own tables. Run migrations with a different user, and keep that password out of the web process. Replicas and notebooks get their own credentials. The connection string is a secret. Store it with the other server secrets.

Parameterize every query

SQL injection happens when user input becomes part of the statement text. An attacker closes your quote and adds their own clause. ORMs prevent a lot of this, until a raw query interpolates a string.

-- safe: the value is a parameter, not part of the SQL text
select * from projects where id = $1 and owner_id = $2

Keep identifiers such as column names out of user input, or allowlist them. A sort parameter of name or created_at can be a map in code. A sort parameter pasted into order by ${req.query.sort} can become a subquery. The same rule applies to Mongo-style queries that merge the request body into a filter. Operators such as $gt or $where should not arrive from the client unfiltered.

Search the codebase for string concatenation next to query, execute, raw, and template literals that include request data. Fix those before you debate indexes.

Enforce ownership in the database when you can

Application checks are required, and a second line helps when a new route forgets them. In Postgres, row level security can require owner_id = current_setting('app.user_id')::uuid on the tables that hold customer data. Set that session variable from the verified user at the start of the request, using a parameter, and use a database role that does not bypass the policy.

If you are not ready for policies, a single query helper that always adds the owner predicate is the minimum. Review any findById a generator added. The pattern and the two-account test are the core of the authorization guide linked above. A policy you never turn on in production, because the migration role bypasses it and the app uses the migration role, protects nothing. Use the restricted app role at runtime so the policy actually runs.

Back up, then restore once

Turn on automated backups and point-in-time recovery if you can get it without a project. Then restore to a new instance and run:

select count(*) from users;

You want a number you recognize. Write down how long the restore took and where the backup lives. Test a delete: if a bug removes rows at 3 p.m., can you recover to 2:50 p.m.? If the answer is "we have a nightly dump and we have never loaded it," you have a file, not a plan.

Encrypt backups with the provider's default or your own key, and restrict who can download them. A backup in a public bucket is a second copy of the breach. Include file storage in the plan if user uploads live outside the database. The launch checklist treats an untested restore as unfinished work for the same reason.

A compose file that reached production

A side project mapped 5432:5432 so a laptop client could connect. The deploy copied that file to a VPS with the tutorial user and password. A scanner found the open port. The repair was a private bind, a new app user, a firewall rule, a rotated password, and a restore that proved the backup held real rows.

If this is your setup, close the firewall and rotate the password before you spend a day on query tuning. Convenience rules from local development need a production file that does not publish the port.

FAQ

Is a managed database safe by default?

Managed databases handle patching and often a private network option. You still choose public access, allowed IPs, the password, and the app user's privileges. If the allowlist includes the whole internet, close it before launch.

Do ORMs make SQL injection impossible?

ORMs make injection unlikely when you stay on their query builder and pass values as data. Raw SQL, string-built where clauses, and some "json filter" helpers accept injection again. Search the code for raw queries and review each one. Also watch for mass assignment, which is an authorization problem that rides in through the same request body.

Should I turn on row level security for a small app?

Turn it on when you can test it with the real app role and two users. A policy that blocks your own support queries will tempt you to connect as superuser and leave it that way. If you cannot operate with the policy yet, keep ownership in a single server helper and add a test that user A cannot read user B. Add policies when that helper starts to sprout exceptions.

How often should I test a restore?

Test once before launch, again after you change backup settings, and on a schedule you can keep, such as once a quarter for a small product. A restore test is a calendar event, not a hope that the toggle still works. If the restore fails, you would rather learn on a scratch instance than on the morning the primary disk dies.

What to do next

From a network that should be blocked, try to connect and confirm the refusal. Log in as the app user and confirm it cannot create a new role. Search for one raw query and make sure the user value is a bound parameter. Restore last night's backup to a scratch database and read a row.

StaySecure reviews risky queries, access checks, and exposed connection strings in the repo. The firewall and the restore test stay in your provider console. Do both before you call the launch real, and secure your database again whenever the network rules change.