Here is the standard procedure to temporarily bypass password authentication for the local postgres superuser on Linux to perform maintenance or reset credentials:
Step 1: Allow local passwordless trust in pg_hba.conf
Insert the trust rule at line 1 of the file so it takes priority over any subsequent md5 or scram-sha-256 directives:
Bash
sed -i '1i local all postgres trust' /var/lib/pgsql/data/pg_hba.conf
Step 2: Reload the configuration
Apply the changes to PostgreSQL without dropping existing client connections:
Bash
systemctl reload postgresql
Step 3: Connect without a password and set/reset credentials
Connect directly to the database engine as the postgres system user:
Bash
su - postgres -c "psql -U postgres"
Inside the interactive psql prompt, set your desired password:
SQL
ALTER USER postgres WITH PASSWORD 'YourStrongPasswordHere';
\q
Step 4: Restore password authentication (md5)
Revert line 1 back to password enforcement to secure local access:
Bash
sed -i 's/^local all postgres trust/local all postgres md5/' /var/lib/pgsql/data/pg_hba.conf
systemctl reload postgresql
Verification:
Confirm that local password enforcement is back in place:
Bash
grep -E "^local.*postgres" /var/lib/pgsql/data/pg_hba.conf
The output should show:
Plaintext
local all postgres md5
