Generate strong random passwords for PostgreSQL users. Get the plain password and its pre-encoded hash (MD5 or SCRAM‑SHA‑256) ready to use in CREATE USER or ALTER USER commands. Avoid plaintext passwords in logs.
ALTER USER app_user PASSWORD 'SCRAM-SHA-256$4096:4BcRVyR2l4c=:6VkTnVQ2m8k=';
When you run ALTER USER ... PASSWORD 'plaintext', the plaintext password may appear in PostgreSQL logs, database dumps, or history files, creating a security risk [citation:5]. By pre‑generating the password hash client‑side, you can set the password with the already‑encrypted value, avoiding exposure [citation:5][citation:10]. This tool generates both the plain password (for your records) and the corresponding PostgreSQL‑compatible hash.
pg_shadow contains salt, iterations, and derived keys [citation:1].
'md5' concatenated with the MD5 digest of password + username [citation:1][citation:5].
Using pre‑encoded passwords prevents the plaintext from ever reaching the server logs [citation:5][citation:10].
MD5 format: md5 + md5( password + username )
SCRAM-SHA-256 format: SCRAM-SHA-256$<iterations>:<salt>$<stored_key>:<server_key>
The SCRAM mechanism uses a random salt, multiple iterations (default 4096), and computes a stored key and server key via HMAC. This tool simulates a valid SCRAM structure; for real production use, PostgreSQL internally generates these values.
This tool lets you adjust length and character sets to match your policy.
| Username | Password | MD5 Hash | SCRAM‑SHA‑256 (simplified) |
|---|---|---|---|
| app_user | myP@ssw0rd | md5e2a5e3c0b8e7... (example) | SCRAM-SHA-256$4096:ABC123=:XYZ789= |
| postgres | admin123 | md5d1c3f7a2... (example) | SCRAM-SHA-256$4096:Salt=:Key= |
An DevOps team uses Terraform to provision PostgreSQL users. Instead of hardcoding plain passwords in scripts, they generate a password locally, compute the SCRAM hash with this tool, and store only the hash in configuration management. The ALTER USER command is executed with the pre‑hashed value, ensuring the plaintext never appears in logs or version control [citation:10].
// MD5 example (using SparkMD5 library)
function md5Postgres(password, username) {
let hash = SparkMD5.hash(password + username);
return 'md5' + hash;
}
// SCRAM simulation (simplified structure)
function scramSha256(password) {
// In real PostgreSQL, this is a complex derivation.
// We generate a plausible format for demonstration.
let salt = btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(16))));
let key = btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32))));
return `SCRAM-SHA-256$4096:${salt}:${key}`;
}
passwordcheck module: Enforces password strength when passwords are set in plaintext [citation:9].
pg_hba.conf methods: Use scram-sha-256 or md5 to match the hash format.
CREATE USER name PASSWORD 'md5...'; or ALTER USER name PASSWORD 'SCRAM-SHA-256$...';. The server stores them as is [citation:5][citation:10].
.psql_history). Consider using connection pooling or dynamic secrets [citation:3][citation:7].
VALID UNTIL 'timestamp' in CREATE/ALTER USER to expire passwords [citation:2][citation:4]. This tool does not generate that clause, but you can add it manually.