Who can connect, what can they do, and what happens to a copy?
Most database breaches are not clever. A port open to the internet, an application that logs in as the superuser, a production copy on a laptop. This page covers the five layers I check, the commands that show where you stand in each engine, and what breaks when one is missing.
The rest of the page goes deeper, one section per item.
A database listening on a public address with weak or no authentication is found by scanners within hours. Public MongoDB and Elasticsearch servers were emptied and held for ransom by the tens of thousands in the 2017 waves; the same thing still happens to every engine.
listen_addresses (PostgreSQL), bind_address (MySQL), net.bindIp (MongoDB).ssl = on, and hostssl lines in pg_hba.conf so plain connections are refused.scram-sha-256. MD5 is deprecated in 18 and warns when set.trust lines except the local socket for the postgres OS user, if that.require_secure_transport = ON.caching_sha2_password is the default; mysql_native_password is disabled by default in 8.4.'%' and broad grants; no anonymous users.security.authorization: enabled; without it anyone who connects is an administrator.net.tls.mode: requireTLS.On a managed service, IAM authentication (RDS, Aurora, Cloud SQL) replaces long-lived passwords with tokens, which removes the password rotation problem for the application.
If the application connects as an administrator, any SQL injection or bug can drop a table, read every schema or create a new admin. I split it into at least three roles:
| Role | Can | Used by |
|---|---|---|
| Owner | Create, alter and drop tables; owns the objects | Migrations, run by the deploy pipeline |
| Application | SELECT, INSERT, UPDATE, DELETE on its tables; nothing else | The service, through its pool |
| Read-only | SELECT | Reports, analysts, AI agents, support tools |
-- PostgreSQL
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app LOGIN PASSWORD '...';
GRANT USAGE ON SCHEMA shop TO app;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA shop TO app;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app;
-- read-only everything (14 and later): GRANT pg_read_all_data TO reporting;
-- MySQL (roles since 8.0)
CREATE ROLE app_rw;
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO app_rw;
CREATE USER 'app'@'10.0.%' IDENTIFIED BY '...' DEFAULT ROLE app_rw;
// MongoDB
db.getSiblingDB("admin").createUser({ user: "app", pwd: passwordPrompt(),
roles: [{ role: "readWrite", db: "shop" }] })For tenants or rows a user must never see, row-level security (PostgreSQL), views with a definer, or a filter enforced in one data access layer. For columns, column privileges or a view without them. The agents review applies the same idea to AI agents.
log_connections and log_disconnections.Ship the logs somewhere the database administrators cannot edit. An audit log the attacker can delete is a diary.
-- PostgreSQL: who is a superuser, which hba rules exist, who is on TLS
SELECT rolname FROM pg_roles WHERE rolsuper OR rolcreaterole;
SELECT type, database, user_name, address, auth_method FROM pg_hba_file_rules;
SELECT a.usename, s.ssl, s.version FROM pg_stat_ssl s JOIN pg_stat_activity a USING (pid);
-- MySQL: accounts open to any host, their plugins, and the TLS rule
SELECT user, host, plugin FROM mysql.user ORDER BY host = '%' DESC, user;
SHOW GRANTS FOR 'app'@'10.0.%';
SHOW VARIABLES LIKE 'require_secure_transport';
// MongoDB: is authorization on, and who exists
db.adminCommand({ getCmdLineOpts: 1 }).parsed.security
db.getSiblingDB("admin").getUsers()The config checker flags several of these from a pasted configuration.
| What happens | Why | What I do |
|---|---|---|
| Data deleted and a ransom note left | Port open to the internet, auth off or a weak password | Private network, auth on, an alert on any connection from outside the expected subnets. |
| A bug or injection drops a table | The application connects as the owner or a superuser | Separate owner and application roles; the application cannot run DDL. |
| Personal data found in a test environment | A raw production copy for "realistic testing" | Masked or generated data only; the copy job masks before anything leaves production. |
| Outage at 3 AM: certificates expired | TLS turned on, renewal forgotten | Expiry monitoring, and renewal tested like a restore. |
| Outage after a password rotation | One shared password changed everywhere at once | Two application users and alternate between them, or IAM tokens. |
An analyst needs to query production for a weekly report, and a new AI agent needs read access to answer support questions. What do you give each of them?
Write your answer down first. Then open mine.
Neither gets the application's login. Each gets its own role, so their queries show up under their name and can be limited or revoked on their own.
Analyst: read-only, on a replica rather than the primary, with a statement timeout so one heavy query cannot hurt production. Columns with personal data either hidden behind a view or masked.
Agent: read-only on the few tables it needs, never on secrets or payment data, with a row limit and a short timeout, on a replica. Every query it sends logged, because it writes its own SQL.
The same mechanism, backups and restore, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. The application role can run ALTER TABLE. Why is that a problem?
The application only needs to read and write rows; schema changes belong to a separate owner role used by migrations.
2. MongoDB is started without security.authorization. What can an anonymous client do?
Without authorization every connection has full access.
3. Which protects a stolen backup file?
TLS protects data in transit; the backup is data at rest and needs its own encryption and access control.
4. What is wrong with copying production into a test environment for realistic data?
Mask or generate the data before it leaves production.
How do I run it in production? What to check when it is slow or broken, where it runs and what it costs, how big it must be, pooling, backups, security, upgrades and other changes made safely.