125

I have just install Postgres 9.3 on Windows 7. The installation completed successfully. It has never asked me to provide the password for postgres user.

The service postgresql-x64-9.3 is up and running. However, I cannot connect: I do not not know the password. I've found the following answer, but it did not help:

similar question on Ubuntu

Community
  • 1
  • 1
AlexC
  • 3,343
  • 6
  • 29
  • 38

9 Answers9

121

[LINUX]

might work for windows too

After installing postgres follow following steps in order to setup password for default system account of Linux execute following in terminal:

user:~$ sudo -i -u postgres
postgres@user:~$ psql

after executing above two commands you will get into postgres shell

Execute this query in postgres shell:

postgres=# ALTER USER postgres PASSWORD 'mynewpassword';

your new password is 'mynewpassword' without quotes and now you can connect with external GUI tools like DBeaver

Vikas Bansal
  • 2,184
  • 2
  • 15
  • 18
  • 5
    This is absolutely working! Thanks – Mark Anthony Libres Oct 20 '21 at 19:09
  • 4
    Really bad answer. What is the password for postgres to allow me to do that? – Owl Oct 25 '21 at 12:48
  • 9
    Just to clarify, psql asks for a password when running it. The issue is i don't know that password, so telling me to bypass this step so i can change the password when i can't get in to run that command isn't helpful for me. – Owl Oct 25 '21 at 12:52
  • This answer works great. Just make sure you haven't changed anything since the installation. – Omar Dulaimi Oct 25 '21 at 20:26
  • @Owl If your system asks for a password when you run $sudo -i -u postgres, it is most likely asking for *your* password since you used sudo. – Chuckk Hubbard Jan 13 '23 at 07:40
  • I can run this just fine, but on attempting to authenticate, no, that password that I set is not accepted. "Peer authentication failed for user "postgres". Right after I supposedly altered the user postgres and set the password. – Chuckk Hubbard Jan 13 '23 at 07:41
  • If there are issues still after following this, try this for an alternative - sudo su postgres psql -p 5460 (different port) – Hari Reddy Jul 11 '23 at 19:56
  • Not working with a fresh Postgres 14 on Ubuntu 22.04 for me. – philburns Aug 28 '23 at 11:37
85

WARNING: trust means exactly that. Anyone who can connect to the PostgreSQL server can control it. If you set trust mode that allows superusers like user postgres (or all users) to connect, they get total control of your PostgreSQL and can probably run shell commands too. You should usually only use it to change the password then restore the configuration back to the auth mode you were using before.


If you used an unattended installer script, the password will be in the script or associated config file.

Otherwise, treat it the same as if you lost/forgot the password rather than never knowing it:

  • Edit pg_hba.conf, setting the auth mode to trust instead of the default md5
  • In the Services control panel restart the PostgreSQL service
  • Connect with psql or PgAdmin or whatever
  • ALTER USER postgres PASSWORD 'mynewpassword';
  • Edit pg_hba.conf again and set the auth mode back to md5
  • Restart PostgreSQL again

pg_hba.conf is in your data directory. By default it'll be %PROGRAMFILES%\PostgreSQL\9.3\data.

To edit it you'll have to use the security tab to give yourself read/write permissions (via a UAC prompt). This might require you to set yourself as the owner of the file.


On unix systems it's more secure to prepend a

local all all peer

line to pg_hba.conf and then sudo -u postgres psql (assuming your PostgreSQL server runs as user postgres) to get an interactive psql session without using a password. That way you don't have to use trust.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • 1
    This doesn't work anymore on newer versions of PostgreSQL. I'm running PostgreSQL 11 on MacOS, the auth mode in `pg_hba.conf` is `trust`, but still I get the authentication error. – ataravati Nov 27 '19 at 18:20
  • 4
    @ataravati Yes, it does. Possibilities: You have edited the wrong `pg_hba.conf` (you have more than one postgres data dir). You forgot to reload the PostgreSQL configuration or restart the PostgreSQL server after editing the config file. There are lines before the entry/entries you edited/added that match so your `trust` entries never get matched. You are connecting over unix socket (`local`) or TCP/IP (`host`) mode, and only edited the config for the other auth mode, try the other connection mode and check both are edited. – Craig Ringer Dec 06 '19 at 04:13
  • 1
    This was the only sensible answer that worked for me - thank you. – Owl Oct 25 '21 at 12:53
24

By default user postgres does not have a password

Start psql and create a password:

  1. sudo -u postgres psql
  2. \password postgres - It will ask you enter a password for user postgres
Karan Shah
  • 345
  • 2
  • 5
  • It's best to cite a reference to back up your answer. – Rohit Gupta Nov 11 '22 at 23:25
  • I am not sure why the below command: ``` postgres=# ALTER USER postgres PASSWORD 'mynewpassword'; ``` returned: ``` ALTER: command not found ``` But your mentioned way worked fine for me. Now I can connect to DBeaver. – Debojyoti Chatterjee Jul 10 '23 at 14:29
18

On initialisation you can access the DB as:

Username: postgres

Password: postgres

Ben Ripka
  • 358
  • 2
  • 5
6

Through trial and error I found that the password for Postgre SQL 10 for the username postgres is "admin". I kept typing in different password until I reached that password. I am using pgAdmin 4 to test out my SQL Statements, POSTGRE SQL 10 is the first server connection set up using localhost.

Demetre Phipps
  • 107
  • 1
  • 3
2

It seems there was no default password, but psql wouldn't accept a lack of a password (fe_sendauth: no password supplied). To get around this, I opened pgAdmin, then in the left sidebar:

  • Servers
  • Login/Group Roles
  • Right click postgres and click Properties,
  • Go to Definition tab
  • Set the password in the Password field

After saving, psql accepted that password. There may have been a switch I could have supplied to have it accept a lack of a password (--no-password?), but the user should probably have a password anyways, so this seemed reasonable.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
1

The simplest solution I've found is just to install PgAdmin and connect to the local server with the current Windows credentials (username + password). Then you can change the password to the postgres user.

Kostia Shiian
  • 1,024
  • 7
  • 12
0

go to control >> computer management >> Locaol users and group >> users >> right click on openpgsvc >> set password. after that now you can access with this password on openpgsvc

lalit bhadouria
  • 57
  • 1
  • 14
-5

step1: Go to control panel
Step2: Click on Administrative Tools
Step3: Click on Computer Management
Step4: There under "Local Users and Groups" Double click on user

Step:5: then right click on postgres and you can set password refer this below image