How to compromise a database (and how to avoid it)

Published on 2026-09-12 by frfrand

The five real ways data leaks, in order of frequency. None involve breaking encryption, and almost all are closed with decisions you already know.

How to compromise a database (and how to avoid it)

Short answer: A database is almost never compromised by breaking encryption. The five real paths, in order of frequency, are: it was left open to the Internet, SQL injection, a leaked credential, an application with excessive privileges, and an unprotected backup. They are closed with known decisions: listen only on localhost, parameterized queries, secrets outside the code, a least-privilege user, and encrypted backups with restricted access.

When people talk about "hacking a database," the mental image is usually someone breaking encryption. In reality, almost no breach happens that way.

These are the real paths, in order of frequency.

1. It was open

The most common, and the least glamorous.

A database port exposed to the Internet, with a weak password or no authentication at all. Entire networks scan the address space looking for exactly that, all day long.

It was almost never opened on purpose: it stayed that way after a test, a migration, or a "I'll open it for five minutes."

It is closed like this: the database listens only on localhost, and remote access is via tunnel or private network. If something doesn't need to be open, it isn't opened.

2. SQL injection

Still relevant after more than twenty-five years.

It happens when user input is pasted into a query as text. If someone writes something that includes quotes and more SQL, that SQL gets executed.

The solution is not to manually escape quotes, nor to filter suspicious words. It is to use parameterized queries: the value travels separately from the query and never becomes part of the SQL.

# vulnerable q = "SELECT * FROM usuarios WHERE mail = '" + mail + "'"

# correcto cur.execute("SELECT * FROM usuarios WHERE mail = %s", [mail])

Every modern ORM does this by default. The risk appears when someone builds a query manually "because the ORM didn't support it".

3. A leaked credential

The database password ended up in a commit, in a log, in a screenshot, in a chat.

This is reduced as follows: credentials outside the code, .env ignored from the first commit, and —most importantly— rotate them as soon as they are suspected to be compromised. Remember that deleting a key from a commit does not remove it from history.

4. Excessive privileges

This one does not open the door, but decides how much is lost.

If your application connects with the administrator user, any of the three paths above grants total access: read everything, modify everything, delete everything.

If it connects with a user who can only read and write to the tables it uses, the same failure has a much smaller scope.

It is the cheapest measure of all and the one most often skipped, because at first "it's more convenient this way".

5. The backup

The database may be perfectly protected and the nightly dump may be in an accessible folder, or in a misconfigured cloud storage, or on someone's laptop.

A backup is a complete copy of your data without any of the database protections. It deserves the same care: encryption, strict permissions, and limited access.

What to look at today

Five concrete questions:

  • Does the database listen on all interfaces or only on localhost?
  • Does your application connect with the superuser?
  • Is there any query built with text concatenation?
  • Where are the backups and who can read them?
  • When was the engine last updated?

None of those questions requires advanced security knowledge. That is exactly the point: most leaks could be avoided with decisions the team already knew how to make.

How to compromise a database (and how to avoid it)
How to compromise a database (and how to avoid it)

Basic security check for a database

Before putting a database into production, check four aspects: whether it listens on a public interface, who can authenticate, what operations each user allows, and where backups are stored. An open port, credentials in Git, or an API without authorization can expose information even if the database is up to date.

If you use PostgreSQL on an ArduMaker VPS, it is common for the application to access via a restricted network and for remote maintenance to be performed through an authorized tunnel, instead of opening port 5432 to the entire Internet. Additionally, create an application user with limited permissions and test a backup restoration. If you install PostgreSQL as an ArduMaker app, the database is born like this: internal only, with its own user without superuser privileges and encrypted connection; opening it to the Internet is optional and by IP list.

Frequently asked questions

What is the most common cause of a database leak? A database exposed to the Internet with weak or non-existent authentication, almost always left open "for a while" and never closed.

Does escaping quotes prevent SQL injection? Not reliably. The defense is to use parameterized queries, where the value travels separately from the query.

Does deleting a password from a commit suffice? No. It remains in the repository history: it must be rotated.

Do backups need to be encrypted? Yes. A dump contains all the data without any of the database's protections.

Related: Connect to PostgreSQL without exposing it · Linux Firewall.

  • seguridad
  • bases de datos
  • SQL injection
  • backups
  • accesos

On ArduMaker

  • Automatic backups: A full live weekly copy, the last 2 kept, manual copies and downloads, and a final copy when the Linux Computer is deleted.

Keep reading

All articles · VPS plans