Users and permissions in the database
How to create a separate user for each application in MySQL and PostgreSQL, grant minimal permissions, and verify that nothing extra is allowed.
Updated August 23, 2026
An application running as root or postgres hands over every database on the machine to an attacker at the first vulnerability. A dedicated user with privileges only on its own schema limits the damage to one project.
MySQL and MariaDB
A user here is a “name and host” pair, and this is the main source of confusion:
CREATE USER 'shop'@'localhost' IDENTIFIED BY 'long-random-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'shop'@'localhost';
FLUSH PRIVILEGES;
'shop'@'localhost' and 'shop'@'%' are two different users with different passwords. Do not create records with the host % unless necessary: it allows login from any address.
Privileges on the structure are needed only during migrations:
GRANT CREATE, ALTER, DROP, INDEX, REFERENCES ON shop.* TO 'shop_migrate'@'localhost';
Separating the application role and the migration role is good practice: a regular query then physically cannot drop a table.
Check:
SELECT user, host FROM mysql.user;
SHOW GRANTS FOR 'shop'@'localhost';
Changing the password and deleting:
ALTER USER 'shop'@'localhost' IDENTIFIED BY 'new-password';
DROP USER 'shop'@'localhost';
PostgreSQL
The role and its privileges on the database and schema:
CREATE ROLE shop WITH LOGIN PASSWORD 'long-random-password';
CREATE DATABASE shop OWNER shop;
REVOKE ALL ON DATABASE shop FROM PUBLIC;
For a read-only role (reports, analytics, monitoring panel):
CREATE ROLE reader WITH LOGIN PASSWORD '...';
GRANT CONNECT ON DATABASE shop TO reader;
GRANT USAGE ON SCHEMA public TO reader;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO reader;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO reader;
The last line is mandatory: without it, the role will not see tables created after the privileges are granted, and in a month the reports will break on new data.
Check:
\du
\l
\dp
What must be locked down
- No application role is a superuser.
- Each project has its own database and its own user.
- Passwords are long and random, and different for different projects.
- External login is allowed only for the addresses that need it — remote access.
- MySQL has no anonymous users and no test database:
mariadb-secure-installationdoes this.
Common mistakes
| Symptom | Cause |
|---|---|
Access denied with the correct password | Another host rule matched, for example ''@'localhost' |
Privileges granted, but SELECT does not work | PostgreSQL lacks USAGE ON SCHEMA |
| The new table is not visible to the role | DEFAULT PRIVILEGES are not set |
| The application deleted everything during a failed migration | The application role had DROP privileges |
| Password is visible in command history | Set passwords from a file, clear ~/.mysql_history |
Store database passwords in the service environment file with
600permissions, not in the code. If compromised, you change one line and restart, not rebuild the whole project.