Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to break into a PostgreSQL database if you forgot the password?

I have a client that has a PostgreSQL database and he cannot remember the password that we used when the database was setup. Is there a way to recover that information so I do not have to blow away his database and start from scratch?

The database is running on a PC.

like image 892
Mark Avatar asked May 28 '09 19:05

Mark


People also ask

What to do if we forgot PostgreSQL password?

If you don't remember your PostgreSQL database password, you can follow the steps below to reset it to a new value: Change the authentication method in the PostgreSQL configuration file pg_hba. conf from md5 to trust and reload the configuration. You should now be able to connect to PostgreSQL with the new password.

Where are PostgreSQL passwords stored?

PostgreSQL database passwords are separate from operating system user passwords. The password for each database user is stored in the pg_authid system catalog. Passwords can be managed with the SQL commands CREATE ROLE and ALTER ROLE, e.g., CREATE ROLE foo WITH LOGIN PASSWORD 'secret' , or the psql command \password .

What is the default password for PostgreSQL database?

Login and Connect as Default User For most systems, the default Postgres user is postgres and a password is not required for authentication.


2 Answers

Step 1: Edit PostgreSQL config file to establish trust relationship to login without password:

vi /var/lib/pgsql/data/pg_hba.conf

Old Line:

local all postgres password

Change it to:

local all postgres trust

Step 2: Restart PostgreSQL Server:

service postgresql restart

Step 3: Change password:

psql -U postgres template1 -c alter user postgres with password ‘newpassword’;

Step 4: Password has been updated. Revert back the original settings of config file:

vi /var/lib/pgsql/data/pg_hba.conf

Old Line:

local all postgres trust

Change it to:

local all postgres password

Step 5: Restart server and use your new password to access PostgreSQL Server.

service postgresql restart

Source

like image 132
biggusjimmus Avatar answered Oct 04 '22 03:10

biggusjimmus


For 9.2, in windows:

Stop the service:

Net stop postgresql-x64-9

Modify the config file, change data/pg_hba.conf, basically md5 to trust:

host all all 127.0.0.1/32 trust

host all all ::1/128 trust

Start the service:

Net start postgresql-x64-9

Execute the sql statement to set your desired password, on a sql console or using psql:

alter user postgres with password ‘newpassword’;

Put back the original config file.

like image 2
dsantaolalla Avatar answered Oct 04 '22 04:10

dsantaolalla