I am running PostgreSQL 9.3 on FreeBSD. FreeBSD uses pgsql
as the default system user for PostgreSQL. My /usr/local/pgsql/data/pg_hba.conf
looks like this:
# TYPE DATABASE USER ADDRESS METHOD local all pgsql peer local all all md5 host all all 127.0.0.1/32 md5 host all all ::1/128 md5
With this configuration I can connect to the database as pgsql
without a password.
$ su pgsql $ psql template1 template1=# \l List of databases ...
That works as intended.
On a remote machine, I have an Ansible task to create a database on the FreeBSD server.
- name: Create the postgresql database postgresql_db: name=mydatabase login_user=pgsql
Executing this task fails with the error Peer authentication failed for user "pgsql"
.
PLAY [web] ******************************************************************** GATHERING FACTS *************************************************************** ok: [host.example.org] TASK: [database | Create the postgresql database] ***************************** failed: [host.example.org] => {"failed": true} msg: unable to connect to database: FATAL: Peer authentication failed for user "pgsql" FATAL: all hosts have already failed -- aborting
Why does this fail when peer authentication for the user pgsql
is clearly working?
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'
The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). This method is only supported on local connections.
This worked for me:
- name: Create postgres database become: true become_user: postgres postgresql_db: name: <database-name>
In your specific case the user might be pgsql
, but I think usually the user is postgres
.
Or with slightly different syntax (from Ansible 1.9) and for user creation (might be helpful for someone)
- name: Create postgres user postgresql_user: name={{ pg_user }} password={{ pg_password }} become: true become_user: postgres
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With