set blank password for PostgreSQL user
Asked Answered
V

4

73

I use postgresql command 'createuser myusername'. It prompts me for password always. If I leave password blank it gives an error that no password is specified.

I simply want to create a user with blank/no password and I'm not able to do it with Postgresql 9 under mac os x.

What steps should I take to create a user with no password. The command always asks me to enter a password.

Vega answered 24/3, 2011 at 15:44 Comment(0)
B
73

Fixing this with createuser won't work. Per the man page for createuser, it says that login will fail if you really need a password and set --no-password. createuser just creates a user. It does not decide how the user will have to authenticate.

Authentication is handled mainly in pg_hba.conf, which decides whether or not to require passwords at login. I've never run PostgreSQL on OS X. On Linux, my distro's stock conf looks like this:

# TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     ident
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

Make sure you have something like the first line for for development access. Note: psql will try to connect with a unix socket if there is no --host option set. The second line would be used if you try to connect with psql --host localhost

Bozo answered 24/3, 2011 at 18:51 Comment(6)
where is the location of file pg_hba.conf on a mac os x or linux box?Vega
For Ubuntu, you can find it at /etc/postgresql/9.0/main/pg_hba.conf (for PostgreSQL 9.0).Mcshane
For OS X 10.6.8, you can find it at /Library/PostgreSQL/8.3/data/pg_hba.conf. Note that it is owned by the postgres user, so you will need to su postgres to edit it.Autostability
The location on CentOS is /var/lib/pgsql/9.3/data/pg_hba.conf.Davisdavison
Using the installer for PostgreSQL 9.4 on OSX 10.10.2, the default location is /Library/PostgreSQL/9.4/data/pg_hba.conf . Note, after editing the method from md5 to ident, I had to restart the db: sudo -u postgres pg_ctl -D /Library/PostgreSQL/9.4/data restartChaste
What does this mean? "Note: psql will try to connect with a unix socket if there is no --host option set"?Maurene
V
39

Use .pgpass

Rather than creating a user with no password, an alternative is to create a .pgpass file with the password in to have it automatically supplied. This will be respected by all applications that use libpq to connect, which is practically all of them except the JDBC driver and the .NET driver.

Here is how to create an example .pgpass file for the user "foobar" and the password "password", with the right file permissions:

echo 'localhost:*:*:foobar:password' >> ~/.pgpass
chmod 600 ~/.pgpass
Vesuvian answered 24/3, 2011 at 18:36 Comment(2)
Worked on my mac, not working on ubuntu though (through WSL)Keil
You my friend are a champion. This is my preferred solution it worked on my Mac where my postgres user had a password set but a script I needed to run doesn't supply one since it can run on anyone's machine.Lioness
K
15

The additional way (I prefere it) to change user's password for PG user is to login to postgres console (as a postgres user on a local pc), and issue SQL request:

$ sudo -u postgres psql -w

# ALTER USER postgres WITH PASSWORD '';
Kersey answered 2/3, 2016 at 16:42 Comment(6)
be aware, this may disable the account if the server requires passwords!Shari
doesn't work for me. Error: psql: fe_sendauth: no password suppliedStandardbearer
@Standardbearer have you setup a password earlier for the system user postgres? Also you have to try with -w i.e. psql -wWenoa
I didn't setup a password for the postgres system user. When I update the password in psql it didn't complain that it can not update the password, but when I try to use the password to login, it's giving me the above error. But funny thing is that it's not working also with other users. Maybe I have to configure psql to accept users without password?Standardbearer
@Standardbearer the ploblem is not in in log to postgres linux/unix userm, but to connecting to the same name db roleWenoa
Aaa yeap, sure. I changed the password in the psql, not to the system user. The problem is that if I set a password, it works (any password), but if I try to change it NULL, then I get the error: Error: psql: fe_sendauth: no password supplied.Standardbearer
L
3
postgres=# ALTER USER postgres WITH PASSWORD '';
NOTICE:  empty string is not a valid password, clearing password
ALTER ROLE
postgres=# 
Loire answered 25/7, 2019 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.