Login to Postgresql to create a new database

I created the attached plabooks to create a new database auth in my Postgresql server. I use the become_user: postgres to not need the pass how say de documentation " * To avoid “Peer authentication failed for user postgres” error, use postgres user as a become_user."
But when I run the playbook, I get the follow error: “msg”: “unable to connect to database: connection to the server failed on socket «/run/postgresql/.s.PGSQL.5432»: fe_sendauth: no password supplied\n”

  • my code!
- name: Postgresql configuration and service management
  hosts: "{{ HOSTS }}"
  remote_user: "{{ remote_user | default('root') }}"
  become: true
  vars:
    auth_db_name: "auth"
    auth_db_user: "{{ auth_db_user }}"
    auth_db_password: "{{ auth_db_password }}"

  tasks:
    - name: Utility present
      ansible.builtin.package:
        name: python3-psycopg2
        state: present

    - name: Create 'auth' database
      become: true
      become_user: postgres
      community.postgresql.postgresql_db:
        name: "{{ auth_db_name }}"
        state: present
2 Likes

Before debugging this Playbook further: are you able to “su” to the postgres user and use “psql” without a password?

2 Likes

Hey Andreas, yes in the CLI I can use the: su - postgres

And can you login into “psql” once you did “su - postgres”?

1 Like

I tested again, and is required the password to psql command, I going to review why

The problem was with the pg_hba.conf
local all postgres trust
This config work for me
Thanks so much for the help

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.