14

I am trying to access postgresql through the command line. However, whenever it is time for me to enter my password, I get the following error: Fatal: password authentication failed for user RMehta. I am pretty sure the reason that password authentication fails is that the user for my database is postgres, and not RMehta.

The only solution I found was using runas in the command line, but I couldn't figure how to get runas to work. Thanks a lot for any advice. I am using windows 7, and postgresql 9.3

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Ravi Mehta
  • 485
  • 1
  • 6
  • 15
  • Can you post exact commands that you issue and also precise output that you get? – vyegorov Oct 17 '14 at 14:47
  • I've tried a bunch of things, but the best I could think of was RUNAS /USER:postgres. The output I get for everything I try is RUNAS USAGE: and then a whole bunch of info on how runas is supposed to work. No error messages. I don't anything I do is even recognized as a command. – Ravi Mehta Oct 17 '14 at 14:55
  • Apparently you are either providing the wrong username/password or your `pg_hba.conf` is not configured correctly: http://stackoverflow.com/search?q=[postgresql]Fatal%3A+password+authentication+failed+for+user+ –  Oct 17 '14 at 17:44

3 Answers3

28

For Unix environnement the command line is

psql -U USERNAME -h localhost dbname

For a Windows environment, you may consider replacing "-" with "/"

-U option able you to choose a user to connect with

-h option able you to connect with the TCPIP protocol, you may consider it useless for Windows

Charkan
  • 831
  • 6
  • 12
  • You can **not** replace `-` (or `--`) with `/` for the parameters in Windows –  Oct 17 '14 at 17:45
3

First make sure your user have a sudo access if not you can use the below command to add your user as sudo user :-

sudo adduser <username> sudo

The change will take effect the next time the user logs in.

Now try running this command :-

sudo -u postgres psql

if that gives you error follow the below steps it should work.

i) Now go to sudo vim /etc/postgresql/<your_postgres_version>/main/pg_hba.conf file and look for line that says :

local   all             postgres                                md5 #peer

and comment that. Just below that line there must be a commented line that says:

local   all             postgres                                peer

or for older versions it'll be :-

local   all         postgres                          ident

Uncomment that line.

ii) Now restart the postgres by using any of these commands :-

sudo /etc/init.d/postgresql restart

OR

sudo service postgresql restart

iii) Now you can simply log into postgres using the following command :

sudo -u postgres psql

iv) once you're in you can create any operation you want to in my case i wanted to create a new database you can do the same using below command :

CREATE DATABASE airflow_replica;
officialrahulmandal
  • 2,473
  • 1
  • 23
  • 31
2

psql -h <hostname> -U <username> -d <dbname>

If all params are correct then it will ask you for db password for username entered earlier.

Jasmeet Singh
  • 325
  • 2
  • 10