psql connection to AWS PostgreSQL 10.4 results in pw auth failed? - postgresql

Question
I set up an AWS RDS PostgreSQL 10.4 database. The dataset is in a private network, therefore, I use a bastion to forward the connection to it. I can then connect using Postico but not using psql.
Details
I installed PostgreSQL locally using brew install postgres. The command output is (and on a AWS instance see update):
❯ psql --version
psql (PostgreSQL) 10.5
Then I use the following SSH command to tunnel:
ssh -q -o "ConnectTimeout 3" -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" -i bastion-key.pem -L 5432:xxxxxx.xxxxx.eu-central-1.rds.amazonaws.com:5432 ec2-user#xx.xx.xx.xx -N
Then I connect using Postico .. everything fine!
Last but not least I try to connect using the following command:
❯ psql \
--host=127.0.0.1 \
--port=5432 \
--username=USERNAME \
--password \
--dbname=DBNAME
Password for user USERNAME:
psql: FATAL: password authentication failed for user "USERNAME"
FATAL: password authentication failed for user "USERNAME"
I use a macOS High Sierra Version 10.13.6 Macbook 13".
Update 2018-12-05:
Tried it also from a Amazon image running Amazon Linux release 2 (Karoo) same result with psql 9 as version 10. Version 10 was installed following this recommendation.

Solved, still a really shitty problem!
I generated my PostgreSQL passwords using secrets.token_urlsafe(90) Python3 method, which resulted in strings including [_-] which psql could not handle.
Using secrets.choice(string.ascii_uppercase) 1 with the right selection solved the problem!
Long story short: Use only characters and digits for your password and you will have no problems!

Related

PostgreSQL 12 authentication error - could not connect to the server

I have a postgresql 12.2 database on a Red Hat linux 8.4 OS, this database is part of a pgpool cluster.
When I run the following command to check the cluster status:
psql -h <dbnode hostname> -p 9999 -U postgres -c 'show pool_nodes'
it gives the below error after I enter the password:
I am sure that the password is correct, what might be the issue here?
I checked pg_hba.conf file and I have this entry already:
host all all <DB_IP_ADDRESS> md5
I tried other methods as well like "trust", but still no luck.
Can someone help me on this please?

PostgreSQL 13.3 installation on MacOS (Big Sur) failed with error: Failed to initialise the database cluster with initdb

Downloaded the PostgreSQL installer from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads, and tried to install it on my new Macbook Pro running Big Sur.
The installation went fine, but it failed when executing the script /Library/PostgreSQL/13/installer/server/initcluster.sh. I traced to the following script:
su - $OSUSERNAME -c "$INSTALLDIR/bin/initdb --pwfile $INSTALLDIR/installer/server/initdbpw.$$ --encoding=utf8 -A scram-sha-256 -U \"$SUPERNAME\" -D \"$DATADIR\""
Apparently, su - postgres does not work because the new service account postgres is not enabled for login. Maybe this script should use sudo -u postgres instead of su - postgres.
I got it working by updating the service account postgres as follows, so it is enabled for login:
sudo dscl . -passwd /Users/postgres 'MyNewPassword'
sudo dscl . -read /Users/postgres
After you verify that the password of the account is no longer "*", you can uninstall and reinstall PostgreSQL, so it can initialize the db, as long as you provide the same password for postgres when you re-install it.

Cannot install postgres with brew. Get "FATAL: role "postgres" does not exist" no matter what command I try

I've tried to install postgres 13.1 on my Mac using homebrew. (Also tried versions 11 & 12 which at one time were installed on this Mac, but encountering same error with those versions now). Am using notes I took from previous installs & of course google/stackoverflow. Tried many things/many times, but always, if I run psql, createuser, createdb (any postgres command), it responds with:
FATAL: role "postgres" does not exist
Also tried just creating a postgres user on my Mac to run install from that account; no luck.
Also tried postgresql install from the enterprisedb.com site. That worked, but seems klunky & seems to rely on .sh scripts. Interestingly, got the 'role postgres does not exist' error after the edb install too, but noticed if I just entered 'postgres' twice on the command line it worked. Example:
/Library/PostgreSQL/13/bin/psql -h localhost -p 5432 -U postgres postgres
This trick doesn't work tho with the brew install.
Understand I need to create the postgres role, but I can't find a command to do this that doesn't complain that the role doesn't already exist.
Any help would be so so appreciated!
Most sites documenting postgres install with homebrew said to use commands like:
psql postgres
or
createuser postgres
Those didn't work for me, but this did:
psql -d postgres -U <myUserName>
Attaching a screenshot to try to summarize the install experience & what worked:
screenshot
Can you try:
/Library/PostgreSQL/13/bin/psql
and then:
CREATE USER postgres SUPERUSER;
?
This document solved it for me: https://enrq.me/dev/2021/01/13/fix-role-postgres-does-not-exist/
The gist: run createuser -s postgres -U <os-username>

PostgreSQL: Can't log into Postgres - sudo broken?

Hey so after I ran into somes issues I decided to uninstall Postgres and re-install it (re install a newer version btw).
I'm trying to access postgres to create a new db for my Rails app so I run su postgresor su _postgres then I am asked a password but nothing that I enter works.
So I tried sudo -u postgres psql and got
sudo: unknown user: postgres
sudo: unable to initialize policy plugin
I tried also to disable SIP (see: here)
but it didn't work either. same results.
I don't know what to do. Any idea ? I am using Mac OS High Sierra 10.13.6.
Thanks to #gordon Davidson and #wildplasser 's help, I found the solution:
sudo -u myusername psql postgres
I had this same challenge when trying to access a PostgreSQL database.
When I run the command below:
sudo -u postgres psql
I get the error below:
sudo: unknown user: postgres
sudo: unable to initialize policy plugin
The issue was that I did not have the PostgreSQL database server installed on the server. The server only had a MySQL database server set up on it.
That's all.
I hope this helps
From your EC2 node issue the following command to test if database is reachable.
/opt/gitlab/embedded/bin/psql -U YourExistingUsername -d template1 -h AWS-RDS-POSTGRES-ENDPOINT

How to connect to PostgreSQL through CLI?

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
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
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;
psql -h <hostname> -U <username> -d <dbname>
If all params are correct then it will ask you for db password for username entered earlier.