Password authentication methods help keep your database safe by checking if users provide the right password before they can connect.
Password authentication methods in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
METHOD = 'password' | 'md5' | 'scram-sha-256' | 'peer' | 'ident' | 'trust' | ...
This syntax is used in the pg_hba.conf file to set authentication methods.
Common password methods are password, md5, and scram-sha-256.
host all all 0.0.0.0/0 md5
host all all 192.168.1.0/24 scram-sha-256
local all all peer
This example sets the password authentication method to SCRAM-SHA-256 for all users connecting from any IP address. After editing pg_hba.conf, you reload the configuration to apply changes.
# In pg_hba.conf file host all all 0.0.0.0/0 scram-sha-256 -- Then reload PostgreSQL configuration SELECT pg_reload_conf();
MD5 is older and less secure than SCRAM-SHA-256, which is recommended for new setups.
Always reload or restart PostgreSQL after changing authentication methods in pg_hba.conf.
Using trust means no password is needed, which is unsafe for production.
Password authentication methods protect your database by requiring users to prove their identity.
Common methods include md5 and scram-sha-256, with SCRAM being more secure.
Set these methods in pg_hba.conf and reload PostgreSQL to apply changes.
Practice
Solution
Step 1: Understand common PostgreSQL password methods
PostgreSQL supports several password authentication methods including md5 and scram-sha-256.Step 2: Compare security levels
SCRAM-SHA-256 is a newer, more secure method than MD5, which is older and less secure.Final Answer:
scram-sha-256 -> Option AQuick Check:
More secure method = scram-sha-256 [OK]
- Confusing md5 as more secure than scram-sha-256
- Choosing 'password' which sends plain text
- Selecting 'trust' which requires no password
pg_hba.conf file?Solution
Step 1: Identify the correct authentication method syntax
Thepg_hba.conffile uses lines like 'host all all address method' to set authentication.Step 2: Match method to SCRAM
To use SCRAM, the method must be exactly 'scram-sha-256'.Final Answer:
host all all 0.0.0.0/0 scram-sha-256 -> Option CQuick Check:
SCRAM method line = host all all 0.0.0.0/0 scram-sha-256 [OK]
- Using 'md5' instead of 'scram-sha-256' for SCRAM
- Confusing 'password' with SCRAM
- Omitting the IP address or using wrong format
pg_hba.conf line: host all all 192.168.1.0/24 md5, what happens when a user connects from IP 192.168.1.15?Solution
Step 1: Analyze the IP range and method
The line applies to IPs in 192.168.1.0/24, which includes 192.168.1.15, and uses md5 authentication.Step 2: Understand md5 authentication behavior
MD5 requires the client to send an MD5-hashed password for authentication.Final Answer:
The user must provide a password hashed with MD5 to authenticate. -> Option DQuick Check:
IP in range + md5 method = MD5 password required [OK]
- Assuming SCRAM is used instead of MD5
- Thinking no password is needed
- Believing connection is rejected without password
host all all 0.0.0.0/0 scram-sha-256 in pg_hba.conf but users still connect without password prompts. What is the likely cause?Solution
Step 1: Check if configuration changes are active
Changes topg_hba.confrequire PostgreSQL reload to take effect.Step 2: Identify why password prompts are missing
If users connect without password prompts, likely the new method is not active due to missing reload.Final Answer:
PostgreSQL was not reloaded after changing pg_hba.conf -> Option BQuick Check:
Config changes need reload = missing reload causes issue [OK]
- Assuming misspelling causes no prompt instead of error
- Ignoring need to reload server
- Thinking IP range affects password prompt
pg_hba.conf achieve this correctly?Solution
Step 1: Understand pg_hba.conf line order and matching
PostgreSQL checks lines top to bottom and uses the first matching rule.Step 2: Set SCRAM for local network first, then md5 for others
Line 1: local network with scram-sha-256; Line 2: all others with md5.Final Answer:
host all all 192.168.0.0/16 scram-sha-256 host all all 0.0.0.0/0 md5 -> Option AQuick Check:
Specific local network first, then general others [OK]
- Reversing line order causing wrong method to apply
- Using 'trust' which disables password
- Assigning md5 to local network instead of SCRAM
