Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres access with www-data and --host argument

I want to access postgres with the www-data user. The command are launched by cli.

My program need to be able to launch this command :

psql --username www-data --host=127.0.0.1 --dbname=dbname

It work perfectly if I remove the --host=127.0.0.1, unfortunately I use a third party program for this command and I can change it.

My pg_hba.conf is standard local all postgres peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
#host   all             www-data         127.0.0.1/32            md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

In postgresql.conf I put

listen_addresses = '*'

Yes it's insecure but I am testing everything. Still doesn't work

In the .pgpass

127.0.0.1:5432:*:www-data:password

I tried all method : peer, ident, md5, password, I had the error everytime.

The error message (in French) :

FATAL:  authentification par mot de passe ?chou?e pour l'utilisateur  << www-data >>
mot de passe récupéré dans le fichier fichier « /var/www/.pgpass »

It means that the authentication by password failed for user www-data. It also reports that it was able to recover the password from pgpass (/var/www/.pgpass).

Thanks for you help, I have no idea what to do.

like image 491
yburon Avatar asked Feb 05 '26 20:02

yburon


1 Answers

Ok that was extremely stupid.

When I made the createuser command, I used -W instead of -P. I misread the createuser --help :(

I realized my mistake when I tried to recreate the www-data another time, and saw that the password was asked only one time instead of two times.

like image 130
yburon Avatar answered Feb 09 '26 08:02

yburon