Sub topic 2
Authenticating Clients
This chapter assumes you have a PostgreSQL or MySQL server running, sudo rights on the host, and basic knowledge of SQL grants. Each section below controls who can reach the server and what they can do after connecting.
Authenticating clients
PostgreSQL checks each connection against pg_hba.conf, its host-based authentication file. The server reads the rules from top to bottom and applies the first rule that matches the connection type, database, user and address.
# pg_hba.conf (TYPE DATABASE USER ADDRESS METHOD)
hostssl appdb appuser 10.0.0.0/24 scram-sha-256This rule accepts only TLS connections, to one database, for one user, from one subnet, checked with SCRAM password hashes. Apply edits with sudo systemctl reload postgresql.
Creating application users with least privilege
Give each application its own login with only the rights it needs. Do not let applications connect as the database owner or as a superuser.
-- PostgreSQL
CREATE ROLE appuser LOGIN PASSWORD 'change-me';
CREATE DATABASE appdb OWNER appuser;
-- MySQL
CREATE DATABASE appdb;
CREATE USER 'appuser'@'10.0.0.%' IDENTIFIED BY 'change-me';
GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'appuser'@'10.0.0.%';The MySQL account matches only clients from the 10.0.0 network. Replace change-me with a strong secret that is stored outside your code.
Limiting access with a firewall
Authentication rejects unknown users, but a firewall rejects unknown hosts before they reach the login step. Allow the database port only from the private subnet.
sudo ufw allow from 10.0.0.0/24 to any port 5432 proto tcp
sudo ufw status numberedFor MySQL, use port 3306 instead.
Encrypting connections with TLS
TLS (Transport Layer Security) is the protocol that encrypts traffic between client and server. Without it, passwords and query results cross the network in readable form. For PostgreSQL, turn on SSL and point the server at a certificate and key.
# postgresql.conf
ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'The key file must be readable only by the postgres user, or the server will not start. Pair this with hostssl rules so plain connections are refused.
Tuning memory and storage
The main memory setting controls the cache the server keeps for table and index pages. In PostgreSQL this is shared_buffers. In MySQL it is innodb_buffer_pool_size. Size it as a share of host RAM, leaving room for the operating system and client connections.
# MySQL configuration file, [mysqld] section
innodb_buffer_pool_size = 2GOn a dedicated PostgreSQL host, a common starting point is about 25 percent of RAM for shared_buffers. Changing it requires a restart. Check memory use with monitoring before you raise it again.
Access path
Exercise
A pg_hba.conf line reads host appdb appuser 0.0.0.0/0 scram-sha-256. What is wrong with it, and how would you fix it?
Show answer
It allows non-TLS connections from any address, which is far wider than needed. Change host to hostssl and the address to the private subnet, such as 10.0.0.0/24.