Reset Your PostgreSQL Password in Simple Steps

Postgres password reset - Photo Credit | olly
Published on: (Updated on: )
If you cannot remember your PostgreSQL password, you can replace it with the simple steps below:
Step 1: Access Your PostgreSQL Command Line
You need to switch to the 'postgres' user on your machine. How you do this depends on your operating system.
On Linux:
Open a terminal and run:
sudo -i -u postgres
This command switches you to the 'postgres' user.
For Windows:
You might not need to switch users. Just open your command prompt or PowerShell as an admin.
On macOS:
Similar to Linux, open your Terminal and switch to the 'postgres' user if you’ve set a separate user. It might not be necessary depending on how you installed PostgreSQL.
Step 2: Open PostgreSQL Command Line Tool
Once you're in the correct user or the command prompt, access the PostgreSQL command line interface, 'psql', by simply typing:
psql
If you can’t access psql without a password, you’ll need to alter the PostgreSQL instance to allow password-less entry momentarily. This step is detailed in subsequent steps.
Step 3: Update the Method for Localhost Connections
This step involves editing the 'pg_hba.conf' file to allow connections without a password temporarily.
Find your 'pg_hba.conf' file. The location depends on your Operating System and how you installed PostgreSQL. The usual locations are '/etc/postgresql/14/main/pg_hba.conf' on Linux, or within your PostgreSQL installation directory eg, 'C:\Program Files\PostgreSQL\12\data' on Windows.
Open 'pg_hba.conf' in a text editor like nano with root priviledge.
Look for lines similar to: