Why isn't psql requiring me to enter a password to access psql for a login role I just created a password for? - postgresql

Just created a login role and subsequently, a database owned by a login role (both with the same name ". I specified a password. Typed psql fplanner and immediately was brought to a console without it having asked from a password. I'm logged into my computer's admin account, so maybe thats it.
Just wondering why it didn't ask me for a password, and when it would (or should), and also possibly how I might force it to prompt for a password every time before being accessed
just created a rails app, and it is also not requiring login information to access the database. Could again be based off the fact that I'm root already I'm not sure
System: OSX El Capitan, PostgreSQL 9.4.4, PgAdmin 1.20.0
Extremely basic question here, but it doesn't appear to have been asked, so I decided to give it a go.

It probably didn't ask because it is set to "trust" (hopefully only for local connections).
While you are in psql and connected to the database run SHOW hba_file; to find out where your pg_hba.conf file is located. Open this file in your favorite text editor. It contains a quite a lot of comments thoroughtly explaning what you can do (lines starting with a # are comments).
In most default installs there are only two actual configuration lines:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Yours I expect to end in trust change them to md5 for normal password based access.

Related

FATAL: password authentication failed for user "postgres" (postgresql 11 with pgAdmin 4)

I recently installed Postgresql 11, during the installation, there's no step to put password and username for Postgres. Now in pgAdmin 4, I wanted to connect the database to server and it's asking me to input password, and I haven't put any in the first place.
Any one knows what's going on. Thank you!
The default authentication mode for PostgreSQL is set to ident.
You can access your pgpass.conf via pgAdmin -> Files -> open pgpass.conf
That will give you the path of pgpass.conf at the bottom of the window (official documentation).
After knowing the location, you can open this file and edit it to your liking.
If that doesn't work, you can:
Find your pg_hba.conf, usually located under C:\Program Files\PostgreSQL\9.1\data\pg_hba.conf
If necessary, set the permissions on it so that you can modify it. Your user account might not be able to do so until you use the security tab in the properties dialog to give yourself that right by using an admin override.
Alternately, find notepad or notepad++ in your start menu, right click, choose "Run as administrator", then use File->Open to open pg_hba.conf that way.
Edit it to set the "host" line for user "postgres" on host "127.0.0.1/32" to "trust". You can add the line if it isn't there; just insert host all postgres 127.0.0.1/32 trust before any other lines. (You can ignore comments, lines beginning with #).
Restart the PostgreSQL service from the Services control panel (start->run->services.msc)
Connect using psql or pgAdmin4 or whatever you prefer
Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'
Remove the line you added to pg_hba.conf or change it back
Restart PostgreSQL again to bring the changes to effect.
Here is an example of the pg_hba.conf file (METHOD is already set to trust):
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
NOTE: Remember to change the METHOD back to md5 or other auth-methods listed here after changing your password (as stated above).
For Windows variant - I too experienced this nasty bug because of pgAdmin for my Windows x64 install of version 9.2. It left my production paralyzed.
In folder C:\Program Files\PostgreSQL\9.2\data or C:\Program Files (x86)\PostgreSQL\9.x\data, you'll find the pg_hba.conf text file.
Find the following lines:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
and change METHOD md5 to "trust" like this:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
From Windows>Run type "services.msc" and enter find the right PostgreSQL instance and restart it.
Your DB security is now blown wide open! Heed the warning to return it back to md5 after changing the user password expiry time to say year 2099 for all the relevant users.
Change the password of default use
ALTER USER postgres WITH PASSWORD 'new_password';
Note: CREATE USER is the same as CREATE ROLE except that it implies LOGIN.
$ psql postgres
postgres=# create user postgres with superuser password 'postgres';
After successfully changing the master password
If you get the same error even after following the master password reset steps
Open your command prompt and execute
psql -U postgres
It will ask you for the password, enter the new password which you set now parallelly open SQL shell(psql) and try again with the new password
I have tried all the above mentioned solutions, trust me northing worked! I have resolved the issue by using following commands
psql -U default
\password
Enter new password:
Enter it again:
my username is : default
This worked perfectly for me.
For Linux user try this
//CHECK POSTGRES IS WORKING OR NOT
sudo systemctl status postgresql
//THIS WILL ACCEPT PORTS
sudo pg_isready
sudo su postgres
//NAVIGATE TO SQL TERMINAL / BASH
psql
//CREATE A NEW USER WITH PASSWORD
CREATE USER shayon WITH PASSWORD 'shayon';
try using psql -U postgres if have put password while installing this is command where you have to use that. Thank you :)
Option 1: If you use trust
Better change only postgres to trust in the pg_hba.conf, then access your db with postgres super user and add other users and passwords with the power of the postgres super user, then change all other peer to md5.
The steps: In the pg_hba.conf, change
local postgres to trust
do not change local all to trust,
instead change local all from peer to md5 - which means that a right password is enough to login.
See this solution in detail at the second answer of 'Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails'.
Option 2: Use md5, no trust needed (recommended)
This way is even easier because you will need to change the pg_hba.conf only once:
Change any local user from peer to md5, usually:
Change local postgres from peer to md5
Change local all from peer to md5
Add a postgres pw with the power of your Linux pw only:
sudo su postgres
psql (or psql -p <port> if you have more than one PostgreSQL)
\password
\q
See the accepted answer and the comments of "Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails".
I solved this problem by changing peer to trust in the file "pg_hba.conf" at local postgres then I restarted the postgres service with the command:
sudo service postgresql restart
That's it.
This particular situation I'm about to mention probably doesn't come up very often, but I was getting this error as well. After looking into it, it was because I had a local postgres instance listening on port 5433, and I was trying to set up a Kubernetes tunnel to a remote PG instance mapped to local port 5433 as well. It turns out the command I was running was attempting to connect to the local instance rather than the remote instance. When I temporarily stopped the local instance, I was able to connect to the remote instance through the tunnel without changing the psql command I was using.
I know this is an old question, but I had the same problem, e.g. no dialog for setting password for Postgres during installation with Postgresql 11.
Instead of doing all the file manipulations suggested in the other answers, I deleted Postgresql 11 and installed Postgresql 12, where I was prompted for setting password during installation.
Loggin to PgAdmin4
Go to
Object > Create > Login/Group Role
Create the "username" that was named in the psql terminal
Create password
Give it all the rights
Save
try the password immediately in the psql terminal.
It worked for me.
Hope this works for you.
You can use the "superuser" password for the first time.
After that you can use Object > Create > Login/Group Role to change the password for the "postgres" user.
I currently had a headhache solving this case. A friend helped me I decided to post my solution here.
Open pg_hba.conf in any text editor (you can find this file in your postgres instalation folder > data);
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.
For those of you who got this error and NONE of these answers helped, I may not have StackOverflow fish for you, but I'll teach you how to fish!
You likely don't have the correct order of lines in the pg_hba.conf file. If you read this PostgreSQL documentation link below, it says this error can be thrown if "no matching entry is found". However, that is NOT always true! Documentation is written by humans and humans make mistakes.
https://www.postgresql.org/docs/current/client-authentication-problems.html
The truth is that a line further up might take precedence, is qualifying and is forcing you to use a password stored in PostgreSQL rather than delegated authentication or some other method. If you are not specifying a password stored in PostgreSQL, then you do not need the LOGIN role attribute. Put a line at the very top of this list with your specific user, authentication protocol, network details and other criteria. Also, many may think that most computers use IPv4. Try IPv6 and you'll be surprised. Once you know the very specific criteria of your issue and place a line at the top, then you have established the ONLY RELIABLE WAY to troubleshoot these pg_hba.conf issues without source code debugging!
Another helpful trick is to create a crapload of Server entries in pg_admin (SQL IDE for PostgreSQL) with all of your users and authentication protocols for testing. When you test different scenarios, you'll instantly know which ones fail.
Also, whenever you change this file, restart the PostgreSQL service, before testing the user.
You're welcome my friend. :)
Follow below stepsif you are using pgAdmin4 and facing error in updating password :
1] Open file "pg_hba.conf" and find "IPv4 local connections"
2] See the value under "Method" column, it must be set to "md5" becase you selected it while installing.
3] Make "md5" value blank and save the file. Restart pgAdmin4 application.
4] Now again set the value back to "md5" and input your password in pgAdmin application.
You should be successfully able to do it.
windown 11 - postgres 14
open pgAdmin4 - click servers
right-click on your windows user name rule, e.g: MyUserName.
definition tab - enter password, click save.
open/re-open terminal
run: psql "postgres:///"
if you get "MyName database doesn't exist" you're good to go

Postgres server password reset not working

I have Postgresql server 8.2 running on a Windows server. I am trying to reset the root postgres account password and just not having any luck. I have so far done the following:
Edited pg_hba.conf to allow:
local all all trust
I then restarted the postgres server so changes could be applied.
Then I opened command prompt and change directory to the bin folder of the postgres installation folder that has all the postgres .exe files.
From what I understand I am supposed to type the following:
psql -U postgres
And at this point it should let me in and should type:
ALTER USER postgres with password 'newpassword';
However, it keeps prompting me for password for user postgres:
And it just does not seem to be working. So from my understanding it seems that the the trust local rule in the pg_hba.conf is not really working correctly.
Update
My pg_hba.conf file only has the following uncommented tags in it:
host all all 127.0.0.1/32 trust
local all all trust
host all all ::1/128 md5
You seem to be asking to change the user on the operating system password. The Postgres user is a user on the the operating system.
Plus you are locked out due to the security hierarchy in Host Based Authentication (HBA)
host all all 127.0.0.1/32 trust
local all all trust
-> host all all ::1/128 md5 <-
NB Loopback Address - ::1/128
::1/128 is the loopback address of the local host which is the equivalent of the 127.0.0.1 in IPv4.
If the postgres user password (as in operating system user) is not set, the postgresq user password is stored
%APPDATA%\postgresql\pgpass.conf
Ref Location of postgresql default user password if not set
On Unix systems, the permissions on .pgpass must disallow any access to world or group; achieve this by the command chmod 0600 ~/.pgpass. If the permissions are less strict than this, the file will be ignored. On Microsoft Windows, it is assumed that the file is stored in a directory that is secure, so no special permissions check is made.
You could update you Host Based Authentication (HBA) file and if you wanted to; change the password for the postgres user on the operating system or windows Then everything should be fine from psql
Hope this helps

Why can't I login to Postgres?

I've used MySQL for years so I might be naively layering my MySQL expectations on to Posgres, but I am hitting a wall. I created a user, flasktut with LOGIN CREATEDB but when I try to log in, I get
psql: FATAL: Peer authentication failed for user "flasktut" -- I tried resetting the password:
postgres=# ALTER USER flasktut WITH PASSWORD 'zx80xb1';
ALTER ROLE
and I'm still seeing the error. I suspect I'm doing something super obvious wrong here?
I think the simple answer to your question is that there are two parts to logging in, to a Postgres database, and you're considering only one.
Logging into Postgres, has two parts:
The outer security check ensures that certain IP addresses / subnets / local or foreign hosts can be (dis)allowed to even 'reach' the Postgres database. The second aspect to that is that a given combination of user / database is allowed to go through. Importantly, your current setup is probably missing this aspect, and although you've seemingly taken care of the point 2 (below), it doesn't matter, because the login attempts aren't even reaching step 2.
Once the login process gets through the outer check, the Database / User itself should be valid / existing and usable.
The first aspect (above) is controlled by the pg_hba.conf file, and what you need to ensure is that when the database parses through the pg_hba.conf file, the 'first' line that is permissive enough to apply to a given login scenario, is what is accepted as the effective 'rule' for that login attempt.
For e.g.
If logging in locally (without 127.0.0.1 or eth0 etc. IP addresses)
# "local" is for Unix domain socket connections only
local all all trust
Should allow any login process to get through. Once you're able to login, try to restrict and make things more secure (since the above would allow every attempt to go through).
Importantly, subsequent lines although may be more secure / apt they wouldn't apply if a previous line in the configuration file already is considered as a candidate for a login attempt. Also, don't forget to restart / reload Postgres after each change to pg_hba.conf.
If you omit the host name, psql will connect via a Unix-domain socket to a server on the local host, or via TCP/IP to localhost on machines that don't have Unix-domain sockets.
So edit the pg_hba.conf file and add/update the lines:
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
Restart the server for the changes to take effect.
Now run psql -d <your_db> -U flasktut
No password should be asked since we set the method to 'trust' and you should be logged in.
When you feel confident with your postgres skills, you should probably change the 'trust' above to something safer, like 'md5'.
There are two things to consider here.
First, the settings in pg_hba.conf need to allow you to connect to whatever database you are trying to connect to. A typical entry in the file looks like this:
host my_db myself 192.168.1.17/32 md5
host all all 192.168.1.0/24 md5
The first line allows a specific user (myself) from a specific remote client (192.168.1.17/32) to connect to a specific database (my_db) over TCP/IP (host). The second line allows any user to connect to any database on the network 192.168.1.0/24, typical network address for home or a small office. Both need to authenticate with a password (md5). You should add a line like either of the above to allow flasktut to even reach the server for establishing a connection. Note that when you change this file you need to restart the server for the changes to take effect.
The second issue is that your new user flasktut needs to have the permission to connect to whatever database s/he wants to connect to:
GRANT CONNECT ON DATABASE some_db TO flasktut;
And then within that database you have to grant privileges to access the objects, either directly or by granting role flasktut membership in a group role that has the required permissions:
GRANT some_role TO flasktut;
If flasktut is going to have her/his own database, you are probably better off as a superuser impersonating user flasktut to create the database and then letting the real user log in to that database her/himself:
SET SESSION AUTHORIZATION flasktut;
CREATE DATABASE some_db; -- somedb is owned by flasktut
RESET SESSION AUTHORIZATION;
i think you have already used localhost port server,so you can create another database in localhost port server(no need to add server in postgres ,create a db directly from that port server)
or
you can check on settings in pg_hba.conf?
https://help.ubuntu.com/stable/serverguide/postgresql.html
The PostgreSQL authentication system is managed in pg_hba.conf. Multiple authentication mechanisms can be configured depending on how the connection is made, to which database and by which user.
What can be surprising coming from a MySQL background is the default configuration often in place in Linux installations: it is using peer authentication by default for local connections.
Here are the default values on a Debian/Ubuntu system:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
peer looks for the current Linux user running psql (or whatever PostgreSQL client). In this case, password authentication is not used, but it relies on the OS providing the user name. This only works locally, using a Unix socket here (which is the default when you don't specify anything to psql).
md5 will ask for a password, but this is only configured for network connections in this configuration.
As a result, if I create a user and a DB as follows (logged on as postgres):
postgres#mymachine:~$ createuser -S -D -R -P myuser
Enter password for new role:
Enter it again:
postgres#mymachine:~$ createdb -E UTF-8 -O myuser mydb
This will try to connect using a unix socket, and fail because I'm still logged on as postgres, not as myuser (which may or may not even exist on that system):
postgres#mymachine:~$ psql -U myuser mydb
psql: FATAL: Peer authentication failed for user "myuser"
(Had I been logged on as myuser in that Linux shell, it would have worked.)
In contrast, if I explicitly specify I want to connect via the network (albeit 127.0.0.1), I am prompted for the password and I can log on:
postgres#mymachine:~$ psql -U myuser -h 127.0.0.1 mydb
Password for user myuser:
psql (9.1.20)
SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.
mydb=>
The trust mechanism is one that presents the most security risks, in that it does not check anything, but the connection mode on that line.
For example:
# This will let any local user connect to any DB via the Unix socket
# without any authentication:
local all all trust
# This will let any user connect to any DB via 127.0.0.1
# without any authentication:
host all all 127.0.0.1/32 trust
# And worse (DO NOT USE):
# This will let any user connect to any DB from anywhere
# without any authentication:
host all all 0.0.0.0/0 trust
There are not many cases besides resetting a lost postgres (admin) password where using trust is justified.
As a side note, you say you've used this to change the password in psql:
postgres=# ALTER USER flasktut WITH PASSWORD 'zx80xb1';
ALTER ROLE
This works indeed, but there are a couple of side effects: the password is now likely to be logged in clear in ~/.psql_history, and it may also have been logged on the server side (wherever its logs are sent).
Within psql, it is generally better to use the \password command:
postgres=# \password flasktut
Enter new password:
Enter it again:
This will not leave a trace of the password in clear in the logs or history.

Postgresql server not asking for password for remote connections

I found my posgresql database server is not asking password for user postgres when remote connecting through pgadmin. I mean this is when I connect to remote database server from my local computer through pgAdmin.
I did add a password in psql, ALTER USER postgres PASSWORD 'mypassword'.
This is my pg_hba.config file:
/usr/local/pgsql/bin/psql -qAt -c "show hba_file" | xargs grep -v -E '^[[:space:]]*#'
local all all trust
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
host all all ::1/128 md5
So, I do not quite understand what is happening here.
Can anyone help with this?
Thanks a lot.
UPDATE:
If i change:
local all all trust
to
local all all md5
Now, local connections (via SSH) will be asked for password ( wasn't asking for password before.) but remote connections will still connect without a password.
Acutally, I tried connecting to this database server by a rails appliaction from another server, without a password, and the rails server started without a problem.
PUTTING RESULT HERE FOR THE CONVENIENCE
The real reason of this issue was the .pgpass file. Mac stored the password locally in the .pgpass file under user home folder. Then every time when user try to login without a password, PostgreSQL will send the password for user.
Official doc here
Thanks for all the comments and answers ppl!
But the real reason of this issue was the .pgpass file. My mac stored the password locally in the .pgpass file under my user home folder. Then every time when i try to login without a password, PostgreSQL will send the password for me.
So that was the issue i was having....
Thanks for all the reply and the comments again!
For more details can refer to here
Reading the documentation at Postgresql.org
https://www.postgresql.org/docs/current/auth-pg-hba-conf.html
I would suggest that you change the user field with the names of the few users allowed to connect remotely:
host all john,charles 0.0.0.0/0 scram-sha-256
host all john,charles ::1/128 scram-sha-256
Further, for security reasons, I would advice that you look into using hostssl and also that you specify the name of the database(s) that can be accessed remotely:
hostsll webapp123 john,charles 0.0.0.0/0 scram-sha-256
And if the remote access is only from specific computers, specify their static IP addresses (if DHCP is used, use a mask accordingly.)
hostsll webapp123 john,charles 1.2.3.4/32 scram-sha-256
This way you only compromise database webapp123, to what users john and charles can do, and only from computer 1.2.3.4.
As mentioned in the documentation, you can have any number of entries, so if you want to add a test server (i.e. your server at home) then you can add one line so it looks like this:
hostsll webapp123 john,charles 1.2.3.4/32 scram-sha-256
hostsll webapp123 henry home-ip/32 scram-sha-256
By not specifying the users, you probably allow any user, including those without passwords and one of them is selected and it works...
Of course, I would strongly advice that you do not name a user who has administration rights in your database unless you also specify his static IP address.

pgadmin3: FATAL: Ident authentification failed for user "postgres"

I'm trying to register new server in pgadmin3 with following settings:
Name: postgres
Host: localhost
Username: postgres
Password: <password which works for psql>
Service: empty or postgres
But it shows error:
FATAL: Ident authentification failed for user "postgres"
I've restarted postgresql service, but to no avail.
Contents of /var/lib/pgsql/data/pg_hba.conf:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 ident
EDIT: Tools -> Server Configuration -> pg_hba.conf is greyed out.
It looks like PgAdmin-III is probably connecting over IPv6 by default, so it's using the ident line that matches the IPv6 address for localhost, ::1/128.
If you want to use password authentication, you probably want:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
I'm not sure why you have the unix domain socket line set to trust, but that's probably OK if it's just a development machine, so leave it unchanged. It's really much safer to have it as ident (if you want the unix user to have to be the same as the Pg user) or md5 (for password auth on local unix sockets) though.
You'll need to edit pg_hba.conf directly in a text editor if PgAdmin-III doesn't have permissions to edit it. You could run PgAdmin-III as user postgres via sudo, but it's way safer (and probably easier) to just use nano or a similar command-line text editor to modify pg_hba.conf.
The password works for psql because psql will, unless told otherwise, connect over a unix domain socket, and you have that set to trust. You'll probably find you could give any password to psql and it'll still work, because it's never being asked to actually give the password, it's just being automatically trusted.
Yes this type of error is seen by every newbie user to pgadmin.
I have found this solution and it worked for me.
sudo -u postgres psql
This will ask for your system password and then you will get the postgres prompt.
and then in psql type below command to change the password.
\password
now enter the new password and re-enter it.
PostGreSQL Account Debugging Steps (Linux Specific):
Make sure you actually have it installed (not just the client, the server too).
Make sure it is running.
Make sure you know where this is - usually in /var/lib/pgsql/data - however this could be anywhere - /var/lib/pgsql/unrelated-instance. Check your postgres process to see which directory (-D argument) this is.
Modify the pg_hba.conf file in the directory from the last step. I have no idea why this step isn't in the postgres documentation.
The specific configuration has been covered in e.g. Jay and Craig Ringer's answer. Make sure to configure both IPV4 and IPV6.
Restart the server.
Test that your configuration worked. Repeat 5-7 until you can login successfully.
Important Don't stop! Now you should configure a more secure password option - postgres may be fine for doing quick local setup, but you want to be using a more secure, configurable authentication mechanism, like LDAP, Kerberos, or GSSAPI. Additionally, you want to make sure you have SSL turned on.