Unable to execute postgres command - postgresql

I'm having difficulty changing the password associated with the postgres user after installing postgres on my windows 10 machine. My apologies in advance as I'm quite unfamiliar with postgres as well as the commands required to work with it.
I've referenced the approved answer in the below article:
FATAL: password authentication failed for user "postgres" (postgresql 11 with pgAdmin 4)
I'm stuck on the step that requires me to
Connect using psql or pgAdmin4 or whatever you prefer
Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
I don't quite understand this step. I've taken the following steps
Open cmd
run psql
The system then asks me for password for username jason. Regardless of what I enter, i get the following message:
psql: error: could not connect to server: FATAL: password authentication failed for user "jason"
At no point do I have an opportunity to enter the following command:
ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
How can I run this command without being asked to enter a password for postgres?
Thanks!

The steps below require that you remember what you did when you installed PostgreSQL.
Locate the data directory where the installation process created the database cluster. By default, that would be a subdirectory of where you installed the software (which is a bad place)
Edit the pg_hba.conf file therein and add this line on top:
host postgres postgres 127.0.0.1/32 trust
Reload or restart PostgreSQL.
Start cmd.exe and enter
psql -h 127.0.0.1 -p 5432 -d postgres -U postgres
If psql is not on your PATH, use the absolute path C:\...\psql.
Use \password to change the password.

Related

Change authentication method for postgres superuser

I am using psql to connect to a PostgreSQL database on Debian 10. I am trying to connect as the postgres user, to the default postgres database. By default, this is using the 'peer' authentication method, which does not require a password.
If I log in using the 'peer' authentication and set a password using the following command:
ALTER USER postgres WITH PASSWORD 'myPassword';
The query executes successfully, however when I edit pg_hba.conf to change the authentication method from:
local all postgres peer
to:
local all postgres scram-sha-256
and restart the server, I get the following error:
~$ sudo -u postgres psql postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
~$
Does anyone know how to do this?
To change the authentication method in PostgreSQL:
Open a terminal window
Change into the postgres bin directory
Example: cd /usr/local/pgsql/bin
Note: Depending on your install environment the path to the bin directory may vary.
Type su – postgres and press Enter. This will change the logged in to the postgres user.
From the bin directory type ./psql
Type:
ALTER USER your_username password 'new_password'; and press Enter. ALTER ROLE should be displayed.
Type \q and press Enter
Open /path_to_data_directory/pg_hba.conf
Example: /etc/postgresql/11/main/pg_hba.conf
Modify the line at the bottom of the config file to resemble one of these examples.
Note: You will probably only have to change the word trust to md5. The line or lines should already exist.
host all postgres peer
host all your_username your.ip your.subnet md5
Save the changes
Restart PostgreSQL service with systemctl restart postgresql.service
Before you assign the password, you probably need to set the password_encryption to "scram-sha-256". Otherwise, you stored the password in the md5 format, and such a password cannot be used to login when pg_hba.conf calls for "scram-sha-256".
The default setting of password_encryption is still md5. It will change to be "scram-sha-256" in v14.
The error message sent to the unauthenticated user is intentionally vague. The error message in the server log file will probably say DETAIL: User "postgres" does not have a valid SCRAM secret. (If it does not, then ignore this answer, and edit your question to tell us what it does say)
You need to 1st in the shell change to be the "postgres" user which you're not doing correctly above:
sudo su - postgres
Then you can do the following as peer auth:
psql -d postgres -U postgres
Also recommend you set a pw for postgres sql user:
\password postgres
& change the authentication method to "md5", not "peer".

Phoenix and Postgres install - not talking

My OS is Fedora 26
I have installed Postgresql and Phoenix.
Postgres has a superuser "postgres" with password "postgres". This is confirmed by running \du in psql.
When I run $ mix ecto.create, I get
** (Mix) The database for Hello.Repo couldn't be created: FATAL 28000 (invalid_authorization_specification): Ident authentication failed for user "postgres"
I suspect it may be a permissions issue. To log into psql requires
$ sudo -u postgres psql postgres
Whereas Phoenix when attempting to use postgres may not have sudo privilages.
$ psql --version
psql (PostgreSQL) 9.6.8
Any thoughts appreciated.
By default the authentication for the postgres database user connecting to the DB locally is to verify that the operating system user is also postgres. This is what the error message refers to as Ident authentication and is why connection after doing sudo -u postgres works.
To connect as the postgres user using another means of authentication you need to edit the pg_hba.conf file. (HBA stands for host based authentication).
The line that allows this will look like this:
local all postgres peer
Add a line that looks like this (without removing the other line!):
local all postgres md5
And you should be able to connect using the password for postgres as well.
If I remember correctly you will need to restart the DB for this to take effect.

postgresql password authentication failure

I have installed PostreSQL, I have the Heroku toolbox, and I'm still trying to just get started.
From my Windows command prompt, I type psql and it asks for a password, and I type in the one I provided when installing PSQL, and it still gives me:
C:\Users\Tina\Desktop\FriendActivity>psql
Password:
psql: FATAL: password authentication failed for user "Tina"
I have even tried:
C:\Users\Tina\Desktop\FriendActivity>psql -U postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
This is a fresh install. First time trying to use it.
I was trying to follow these steps here:
https://devcenter.heroku.com/articles/heroku-postgresql#local-setup
but
export DATABASE_URL=postgres:///$(whoami)
does not work:
C:\Users\Tina\Desktop\FriendActivity>export DATABASE_URL=postgres:///$(whoami)
'export' is not recognized as an internal or external command,
operable program or batch file.
I have been trying to get this set up to use for a class project that uses a database for a few days now... and I cant seem to get this to work.
the project we are working on is here:
https://github.com/rwprice31/FriendActivity
My part is getting the database functionality working.
You are following instructions written for Linux or Mac OS X. They will not work on Windows.
The Windows equivalent of:
export DATABASE_URL=postgres:///$(whoami)
is
set DATABASE_URL=postgres://localhost/databasename
where you should replace databasename with the name of the database you wish to connect to.
As for:
> psql -U postgres
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
I can only assume that you made a mistake entering the password when you set it up, or you had an old PostgreSQL data directory already in place from a past install.
Either way, you'll need to reset the password. There are many instructions here for doing that, so I won't repeat them.
Regarding:
>psql
Password:
psql: FATAL: password authentication failed for user "Tina"
Presumably there is no user named Tina in PostgreSQL. The installer would usually only create a postgres user when installed.

Openbravo postgres installation

I am trying to get a copy of Openbravo on Ubuntu for development. I am following the official guide and I get stuck by a Postgres error.
I have successfully downloaded the source code through the mercurial commands. ant setup also worked for me.
When I run the Wizard I write this information.
The problem comes when I run ant install.source:
BUILD FAILED
/home/User/openbravo/build.xml:734: The following error occurred while executing this line:
/home/User/openbravo/src-db/database/build-create.xml:50: The following error occurred while executing this line:
/home/marcguilera/openbravo/src-db/database/build-create.xml:77: org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:136)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
The stacktrace is longer.
I)
First we need to change the PostgreSQL postgres user password else we will not be able to access the server. As the “postgres” Linux user, we will execute the psql commands below.
In a terminal, type:
$ sudo -u postgres psql postgres
Set a password for the "postgres" database role using the command:
\password postgres
II) Create database
To create the first database, which we will call leo, simply type:
sudo -u postgres createdb leo
III) install Server Instrumentation (for PgAdmin) for Postgresql 9.1
PgAdmin requires the installation of an add-on for full functionality. The "adminpack" addon, which it calls Server Instrumentation, is part of postgresql-contrib, so you must install that package.
Then to activate the extension,
For "Postgresql 9.1"+ install the adminpack "extension" in the "postgres" database:
$ sudo -u postgres psql
then
CREATE EXTENSION adminpack;
IV) Using pgAdmin III GUI
To get an idea of what PostgreSQL can do, you may start by firing up a graphical client. In a terminal type :
pgadmin3
You will be presented with the pgAdmin III interface.
* Click on the "Add a connection to a server" button (top left).
In the new dialog:
enter the address **127.0.0.1**
host is **jdbc:postgresql://localhost:5432/postgres**
the default database ("leo" in the example above)
your username ("postgres") and your password.
One more step is required in order to allow pgAdmin III to connect to the server, and that is to edit pg_hba.conf file and change the authentication method from peer to md5 (Will not work if you have not set the password.):
sudo nano /etc/postgresql/9.1/main/pg_hba.conf
and change the line
# Database administrative login by Unix domain socket
local all postgres peer
to
# Database administrative login by Unix domain socket
local all postgres md5
Now you should reload the server configuration changes and connect pgAdmin III to your PostgreSQL database server.
sudo /etc/init.d/postgresql reload
With this GUI you may start creating and managing databases, query the database, execute SQl etc.
https://help.ubuntu.com/community/PostgreSQL
v) Run openbravo with postgres
Just adjust the db name, user name and the passwords.
Hope that helps.

pgAdmin:Password authentication failed [duplicate]

I have installed PostgreSQL 8.4, Postgres client and Pgadmin 3. Authentication failed for user "postgres" for both console client and Pgadmin. I have typed user as "postgres" and password "postgres", because it worked before. But now authentication is failed. I did it before a couple of times without this problem. What should I do? And what happens?
psql -U postgres -h localhost -W
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
If I remember correctly the user postgres has no DB password set on Ubuntu by default. That means, that you can login to that account only by using the postgres OS user account.
Assuming, that you have root access on the box you can do:
sudo -u postgres psql
If that fails with a database "postgres" does not exists error, then you are most likely not on a Ubuntu or Debian server :-) In this case simply add template1 to the command:
sudo -u postgres psql template1
If any of those commands fail with an error psql: FATAL: password authentication failed for user "postgres" then check the file /etc/postgresql/8.4/main/pg_hba.conf: There must be a line like this as the first non-comment line:
local all postgres ident
For newer versions of PostgreSQL ident actually might be peer. That's OK also.
Inside the psql shell you can give the DB user postgres a password:
ALTER USER postgres PASSWORD 'newPassword';
You can leave the psql shell by typing CtrlD or with the command \q.
Now you should be able to give pgAdmin a valid password for the DB superuser and it will be happy too. :-)
The response of staff is correct, but if you want to further automate can do:
$ sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
Done! You saved User = postgres and password = postgres.
If you do not have a password for the User postgres ubuntu do:
$ sudo passwd postgres
This was frustrating, most of the above answers are correct but they fail to mention you have to restart the database service before the changes in the pg_hba.conf file will take affect.
so if you make the changes as mentioned above:
local all postgres ident
then restart as root ( on centos its something like service service postgresql-9.2 restart )
now you should be able to access the db as the user postgres
$psql
psql (9.2.4)
Type "help" for help.
postgres=#
Hope this adds info for new postgres users
Edit the pg_hba.conf file, for Debian on /etc/postgresql/9.3/main/pg_hba.conf and for Red Hat/IBM derivates at /var/lib/pgsql/9.4/data/pg_hba.conf
Change all authentication methods to trust.
Change Linux Password for postgres user.
Restart Server.
Login with psql -h localhost -U postgres and use the just set Unix password.
If it works you should re-set the pg_hba.conf file to values with md5 or ident methods and restart.
For those who are using it first time and have no information regarding what the password is they can follow the below steps(assuming you are on ubuntu):
Open the file pg_hba.conf in /etc/postgresql/9.x/main
sudo vi pg_hba.conf
2.edit the below line
local all postgres peer
to
local all postgres trust
Restart the server
sudo service postgresql restart
Finally you can login without need of a password as shown in the figure
Ref here for more info
When you install postgresql no password is set for user postgres, you have to explicitly set it on Unix by using the command:
sudo passwd postgres
It will ask your sudo password and then promt you for new postgres user password.
Source
Try to not use the -W parameter and leave the password in blank. Sometimes the user is created with no-password.
If that doesn't work reset the password. There are several ways to do it, but this works on many systems:
$ su root
$ su postgres
$ psql -h localhost
> ALTER USER postgres with password 'YourNewPassword';
As a rule of thumb: YOU SHOULD NEVER EVER SET A PASSWORD FOR THE POSTGRES USER.
If you need a superuser access from pgAdmin, make another superuser. That way, if the credentials for that superuser is compromised, you can always ssh into the actual database host and manually delete the superuser using
sudo -u postgres -c "DROP ROLE superuser;"
Once you are in your postgres shell, Enter this command
postgres=# \password postgres
After entering this command you will be prompted to set your password , just set the password and then try.
If you are trying to login postgres shell as postgres user, then you can use following commands.
switch to postgres user
# su - postgres
login to psql
# psql
Hope that helps
Ancient thread, but I wasted half a day dealing with this in 2020, so this might help someone: Double-check your postgres port (on Ubuntu, it's in /etc/postgresql/9.5/main/postgresql.conf). The psql client defaults to using port 5432, BUT in my case, the server was running on port 5433. The solution was to specify the -p option in psql (e.g. psql --host=localhost --username=user -p 5433 mydatabase).
If you leave off the --host parameter, psql will connect via a socket, which worked in my case, but my Golang app (which uses TCP/IP) did not. Unfortunately, the error message was password authentication failed for user "user", which was misleading. The fix was to use a url connection string with the port (e.g. postgres://user:password#localhost:5433/mydatabase).
My setup was Ubuntu 18.04 on Digital Ocean, with postgres 9.5 installed via apt-get, so not sure why this happened. Hope this saves you some time.
I faced the same error on Windows 10. In my case, when I setup the Postgres, my username was postgres by default.
But when I ran the command psql, it as showing my the username as jitender which is my machine name, and I don't know why this username had been setup.
Anyway to solved it, I did the following steps:
Run the command psql --help
In the output, look for the Connection Option, here you will see your default user, in my case it as jitender.
You will also get the command to set the anoter username, which should be psql --username postgres. You set the username whatever you require, and that's all, problem got solved.
If you see error
FATAL: password authentication failed for user "postgres"
and you are sure that your password is correct, check that the password has any special characters, especially "%" or slashes.
In my case, it was "%" in the password string. After removing this symbol, everything works fine.
Here are some combinations which I tried to login:
# login via user foo
psql -Ufoo -h localhost
sudo -u postgres psql postgres
# user foo login to postgres db
psql -Ufoo -h localhost -d postgres
Time flies!
On version 12, I have to use "password" instead of "ident" here:
local all postgres password
Connect without using the -h option.
First of All password crate
ALTER USER postgres with encrypted password 'postgres';
then service restart:
sudo systemctl restart postgresql.service
End.
Follow these steps :
sudo -u postgres -i
psql
\password postgres
After that, enter your password twice.
Then use that password in the pgAdmin4.
I was also faced this issue while login the postgres. I was followed the below steps and able to login with postgres and pgadmin.
Step1: Open Postgres using terminal.
sudo su postgres
Step2: Open psql.
psql
Step3: Reset the password of user
ALTER USER user_name WITH PASSWORD 'new_password';
Step4: Give the permission on database to user.
GRANT ALL PRIVILEGES ON DATABASE my_database TO db_user;
I just wanted to add that you should also check if your password is expired.
See Postgres password authentication fails for details.
In my case, Ubuntu 20.04 Postgresql 12 was using the wrong port.
I've checked /etc/postgresql/12/main/postgresql.conf and realized it was 5433 instead of 5432.
The answer is #diego
I want to add some explanations of how I fixed error and I hope it will help other folks:
ERROR: password authentication failed for user "postgres"
On Window
Make sure you download Postgres software, install it, create and confirm password
and make sure its not complicated with some symbols and characters.
Open window, click SQL Shell (PSQL) and access it and create database
Create connection string like
postgres://postgres:your_password#localhost:port/your_database
On WSL
Follow Microsoft documentation
After successful installation
// Open postgres
su postgres
// Type psql and hit enter
psql
// Create a user postgres if not exist or any other user you want
CREATE USER your_user_db WITH PASSWORD 'match_password_with_db_password';
// Give user password same as the one you set up for postgres db
ALTER USER your_user_db WITH PASSWORD 'match_password_with_db_password';
// Restart the server
sudo service postgresql restart
i had a similar problem.
Ubuntu was left me log in in console with any password for superuser.
Except when i connected with -h localhost in psql line command.
I Observed too that "localhost:8080/MyJSPSiteLogIn" - showed: Fatal: autentication error with user "user".
pg_hba.conf was ok.
I noted had two versions of postgres running in the same service.
Solved - uninstalling inutil version.
I had faced similar issue.
While accessing any database I was getting below prompt after updating password
"password authentication failed for user “postgres”" in PGAdmin
Solution:
Shut down postgres server
Re-run pgadmin
pgadmin will ask for password.
Please enter current password of mentioned user
Hope it will resolve your issue
This happens due to caching.
When you run, php artisan config:cache, it will cache the configuration files. Whenever things get change, you need to keep running it to update the cache files. But, it won't cache if you never run that command.
This is OK for production, since config don't change that often. But during staging or dev, you can just disable caching by clearing the cache and don't run the cache command
So, just run php artisan config:clear, and don't run the command previously to avoid caching.
Check original post
Password authentication failed error on running laravel migration
In my case, its Password was longer than 100 characters. Setting it to a smaller character password worked.
Actually I am wondering is there a reference somewhere to that.
Please remember if you have two versions of Postgres installed you need to Uninstall one of them, in my case on MacOS I had one version installed via .dmg and one via brew.
What worked for me was to uninstall the one installed via .dmg using the following steps
Go to /Library/PostgreSQL/13.
Open uninstall-postgres.app.
then try
psql postgres
it should work.
Answer given is almost correct just missing some pointers which i'll be taking care of in my solution
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.
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;
In my case it was so simple! I was taken error in application JAVA Spring because I needed remember the Database Superuser, it is showed during the install process PostgreSQL, in my case the datasource would be postgres. So, I added correctly the name and it works!
Open pg_hba.conf in any text editor (you can find this file in your postgres instalation folder);
Change all the methods fields to trust (meaning you don't need a password for postgre);
Run in your console this comand:
"alter user postgres with password '[my password]';" | psql -U postgres
(meaning to alter some user password for [my password] for the user as parameter -U postgres)
Et voilà (don't forget to change back the method from trust for the one that should be best for you)
I hope this help someone someday.
I hope this will help you short of time.
You can change the password of postgres sql by using bellow command.
Command
sudo -u postgres psql
And next you can update the password
Command
Alter user postgres password 'YOUR_NEW_PASSWORD';