Home > SQL, Tech > PostgreSQL: Reset root password

PostgreSQL: Reset root password


I had an old installation of postgreSQL on my Win32 XP box. I never really used it until today when I was forced to ditch MySQL in favor of postgreSQL. But the classical problem now:  I had forgotten the password for my default postgres account. Since I had no other user(role) created, this meant  had no choice but to forcefully reset my postgres password.

Unfortunately people at postgreSQL seem to be suffering from a widespread tendency of treating a windows user as someone who needs to be punished for committing the cardinal sin of opting windows. I remember several of my batchmates having had their brains fried trying to install postgreSQL on their windows machines (before I stepped in to save them, of course :D).

Anyway, enough digression! Here is how it worked out for me:

  1. Get the server running:
    pg_ctl.exe start -D "C:\Program Files\PostgreSQL\8.3\data"
  2. Open the file
    "C:\Program Files\PostgreSQL\8.3\data\pg_hba.conf"

    Initial Content:

    # TYPE DATABASE  USER CIDR-ADDRESS  METHOD
    
    # IPv4 local connections:
    host all all  127.0.0.1/32 md5
    # IPv6 local connections:
    host all all ::1/128 md5

    Changed to:

    # TYPE DATABASE USER CIDR-ADDRESS METHOD
    
    # IPv4 local connections:
    host all all 127.0.0.1/32 trust sameuser
    # IPv6 local connections:
    host all all ::1/128 trust sameuser
  3. reload the server config:
    pg_ctl.exe reload -D "C:\Program Files\PostgreSQL\8.3\data"
  4. run:
    psql -U postgres

    At this point if you get this error:

    psql: FATAL:  role "postgres" does not exist

    do this:

    createuser.exe -s postgres
  5. run this to start command line client:
    psql -U postgres
  6. run SQL to change password:
    postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';

    Note that the inverted commas around the password are required but are not a part of the password

  7. Rollback the changes made in step 2
  8. reload the server config again as in step 3
  9. Curse the postgre config for one last time
  10. Get going!

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
#host    all         all         127.0.0.1/32          trust sameuser
# IPv6 local connections:
host    all         all         ::1/128               md5
#host    all         all         ::1/128               trust sameuser

  1. March 4, 2010 at 2:59 am

    Informative post. I really enjoyed it.

  2. andrew davis
    November 12, 2010 at 12:41 am

    I am a windows user, can I use the steps outlinned above to reset my root password?

    If so, where do I start.

  3. November 12, 2010 at 8:23 am

    Andrew, the steps described above are for windows so starting from step 1 would be my suggestion 🙂

  4. January 3, 2011 at 7:55 pm

    Hello

    i want to mention that the last line worked for me:
    postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD ‘mypassword’;

  5. PK
    April 13, 2011 at 1:18 pm

    Thank You 🙂

  6. codemonkey
    September 14, 2011 at 11:24 pm

    Assuming you don’t need the databases contained therein, would removing and reinstalling postgreSQL (and/or pgAdmin) be sufficient?

  7. DLS
    September 21, 2011 at 11:30 pm

    THANKS A LOT! (yes, i’m yelling ;))

  8. Mario Grasso
    January 20, 2012 at 9:56 am

    I got the following error at step 6 … any ideas on how to fix this?

    Execution of PostgreSQL by a user with administrative permissions is not
    permitted.
    The server must be started under an unprivileged user ID to prevent
    possible system security compromises. See the documentation for
    more information on how to properly start the server.

  9. May 4, 2012 at 2:40 pm

    it doesnt work when executing: createuser.exe -s postgres

  10. May 8, 2012 at 3:06 pm

    Helpful, works in 9.0 too. Thanks

  11. November 3, 2014 at 3:48 pm

    Reblogged this on Eknaprasath.

  12. February 10, 2015 at 7:50 am

    Awesome blog post dude. Like the effort.

  1. December 7, 2012 at 2:53 am

Leave a comment