psql authentication fails with --host=localhost - postgresql

With the command:
psql --dbname=mattermost --username=mmuser --password
then if I place the password it succeeds, but if I write:
psql --host=localhost --dbname=mattermost --username=mmuser --password
then the same password gives authentication fails
How can I resolve this?

So in this one try using
psql --host=127.0.0.1 --dbname=mattermost --username=mmuser --password
check the pg_hba.conf for further info. So if below line in your pg_hba.conf file
host all all localhost trust
then it will start working with localhost also

Related

How to connect to an alternative local postgresql cluster for the fist time?

In Ubuntu 16.04 I created second postgres database cluster, called cmg, with a local user as the admin user:
pg_create -u "local_username" -g "local_usergroup" -d /path/to/data/dir 9.5 cmg
The cluster was started with:
pg_ctrlcluster 9.5 cmg start
which ran successfully (pg_lsclusters show both are online)
The problem is I cannot connect to the cluster using psql as is normally done.
I tried using:
psql -h 127.0.0.1 -w -p5433 -U local_username
which fails with:
psql: fe_sendauth: no password supplied"
Is there any way to connect to the specific cluster?
use psql -h your_socket_dir -p5433 -U postgres to connect locally (uses peer auth by default - thus high chahce to login wothout password)
once logged in - set up password (create user if needed) and use it connecting remotely
psql -h 127.0.0.1 -p5433 -U local_username
in your connect string you had -w which is never ask for a password https://www.postgresql.org/docs/current/static/app-psql.html which would by default work only for local connections
I think the default pg_hba.conf when you start up a new cluster expects you to authenticate with peer connections, so you need to change user to your local user before connecting
[root#server~]# su - local_username
>> Enter password:
> password
[local_username#server~]# psql -h 127.0.0.1 -p 5433
You can check your pg_hba.conf file in /path/to/data/dir/pg_hba.conf to see how it expects you to authenticate.
Alternatively, if you cannot get access as your 'local_username' then instead su to postgres user in the instructions above and it should work

Heroku pg:push psql: FATAL: password authentication failed for user

I know similar questions have been asked but none of the solutions have worked. I am trying to push my local db to my Heroku db, and I keep getting psql: FATAL: password authentication failed for user "windows username".
I am on windows, so I tried SET PGUSER=postgres SET PGPASSWORD=password
Then ran heroku pg:push localdb DATABASE_URL --app herokuapp
But am still getting this stupid password error. The thing is it still looks like it is using my windows user name and not postgres username.... how do I resolve this?
Thanks to Heroku support I was finally able to get this to work. So for Windows users, these are the steps:
First you want to dump your local database out to a dump file:
pg_dump --verbose -F c -Z 0 -U postgres -h localhost -p 5432 yourdbname > local.dump
Then you want to grab the connection string from your heroku application config vars:
heroku config:get DATABASE_URL
Then you want to pick out the username / hostname / databasename parts from the connection string, ie: postgres:// username : password # hostname : port / databasename
One warning, running this against a production database with real data is something you want to avoid so be careful with pg_restore. When running this manually you run the risk of mangling your data without the CLI check, so you may want to manually verify that the target database is empty first.
pg_restore --verbose --no-acl --no-owner -U username -h hostname -p 5432 -d databasename < local.dump
Then when prompted for a password, just paste in the password from the connection string
Run SET PGUSER=postgres. This is important as otherwise heroku will use a different user and your password will not work.
Run heroku pg:push localdb DATABASE_URL --app herokuapp
Enter your password for postgres when it prompts.
I just faced the exact same problem and was successful in resolving this.
Rather than use the single line 'heroku pg:push' command with / without username/password, I relied instead on 2-steps:
Step-1: pg_dump
Step-2: pg_restore
This as pointed out by https://stackoverflow.com/users/4051445/na-peters above
as well as briefly hinted by Heroku at:
https://devcenter.heroku.com/articles/heroku-postgresql#pg-psql
Key thing is the password to enter is NOT the password you'd use when accessing your local Postgresql database. Instead, the password is a 64-character string you will obtain from:
heroku config:get DATABASE_URL -a
To extract it follow instructions by NA Peters above (it is the y string between : and # in the postgres://xxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyy#hhhhhh:5432/dddddd
Expect it to work
For you Windows PowerShell folks out there struggling with the restore, try:
Get-Content -raw local.dump | pg_restore -F c --verbose --no-acl --no-owner -U <username> -h <hostname> -p 5432 -d <databasename>
Then paste the password in when prompted.

Can connect to postgres through localhost without password but not through 127.0.0.1. Why?

I have Kubuntu 14.10 desktop with PostgreSQL 9.4 database installed.
I have changed the password of the postgres user in the database by executing the SQL:
ALTER USER postgres PASSWORD 'password';
And I can connect to DB server by psql -h localhost -U postgres -W and giving that password but I can also connect without a password requirement by simply psql -h localhost -U postgres.
On the other hand, if I run psql -h 127.0.0.1 -U postgres it prompts me for the password set before.
What is the difference between the localhost and 127.0.0.1 hosts and their login method? Where is it set? I see no localhost-related entries in the pg_hba.conf file.
The behavior you see might be caused by a password file. The password file is conventionally named ~/.pgpass on Unix systems, but a different filename can be given through the PGPASSFILE environmental variable.
I think a password file that contains a line for "localhost", but does not contain a line for "127.0.0.1" will show the behavior you're seeing. My own ~/.pgpass file contains this line.
localhost:*:*:postgres:password
Here's what happens when I try to connect just like you did.
$ psql -h localhost -U postgres
psql (9.3.5)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
sandbox=# \q
$ psql -h 127.0.0.1 -U postgres
Password for user postgres:
Adding the line 127.0.0.1:*:*:postgres:password to ~/.pgpass lets me log in using 127.0.0.1 and no password.
$ psql -h 127.0.0.1 -U postgres
psql (9.3.5)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
sandbox=#

Using psql to connect to PostgreSQL in SSL mode

I am trying to configure ssl certificate for PostgreSQL server. I have created a certificate file (server.crt) and key (server.key) in data directory and update the parameter SSL to "on" to enable secure connection.
I just want only the server to be authenticated with server certificates on the client side and don't require the authenticity of client at server side. I am using psql as a client to connect and execute the commands.
I am using PostgreSQL 8.4 and Linux. I tried with the below command to connect to server with SSL enabled
psql "postgresql://localhost:2345/postgres?sslmode=require"
but I am getting
psql: invalid connection option "postgresql://localhost:2345/postgres?sslmode"
What am doing wrong here? Is the way I am trying to connect to server with SSL mode enabled is correct? Is it fine to authenticate only server and not the client ?
psql below 9.2 does not accept this URL-like syntax for options.
The use of SSL can be driven by the sslmode=value option on the command line or the PGSSLMODE environment variable, but the default being prefer, SSL connections will be tried first automatically without specifying anything.
Example with a conninfo string (updated for psql 8.4)
psql "sslmode=require host=localhost dbname=test"
Read the manual page for more options.
psql --set=sslmode=require -h localhost -p 2345 -U thirunas \
-d postgres -f test_schema.ddl
Another Example for securely connecting to Azure's managed Postgres database:
psql --file=product_data.sql --host=hostname.postgres.database.azure.com --port=5432 \
--username=postgres#postgres-esprit --dbname=product_data \
--set=sslmode=verify-full --set=sslrootcert=/opt/ssl/BaltimoreCyberTrustRoot.crt.pem
Well, you could provide all the information with the following command in CLI, if a connection requires in SSL mode:
psql "sslmode=verify-ca sslrootcert=server-ca.pem sslcert=client-cert.pem sslkey=client-key.pem hostaddr=your_host port=5432 user=your_user dbname=your_db"
Found the following options useful to provide all the files for a self signed postgres instance
psql "host={hostname} sslmode=prefer sslrootcert={ca-cert.pem} sslcert={client-cert.pem} sslkey={client-key.pem} port={port} user={user} dbname={db}"
On psql client v12, I could not find option in psql client to activate sslmode=verify-full.
I ended up using environment variables :
PGSSLMODE=verify-full PGSSLROOTCERT=server-ca.pem psql -h your_host -U your_user -W -d your_db
psql "sslmode=require host=localhost port=2345 dbname=postgres" --username=some_user
According to the postgres psql documentation, only the connection parameters should go in the conninfo string(that's why in our example, --username is not inside that string)
psql -h <host> -p <port> -U <user> -d <db>
and update /var/lib/pgsql/10/data/pg_hba.conf to change the auth method to cert. Check the following link for more information:
https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
Another pattern that worked with v8 is
psql -h host_name -p port -U user_name "dbname=db sslmode=require"

Redmine postgresql FATAL: Ident authentication failed for user "redmine"

Following the these instructions (using postgresql instead of mysql):
http://www.redmine.org/projects/redmine/wiki/Redmine_on_CentOS_installation_HOWTO
when run this command :
RAILS_ENV=production bundle exec rake db:migrate
rake show an error message:
FATAL: Ident authentication failed for user "redmine"
??
It should not be necessary to modify pg_hba.conf for redmine. First check that your database role is configured correctly:
su - postgres
psql -d redmine -U redmine -h localhost
<password challenge>
If the password is accepted, then the next step is to confirm that you have libopenssl-ruby installed.
You should have the postgres user name the same as your linux username
It's easier than modifying "pg_hba.conf" file
This works for me:
psql redmine redmine -h localhost
The -h localhost part makes the difference.