# Login to Postgresql to create a new database

**URL:** <https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364>\
**Category:** Get Help\
**Tags:** collections, postgresql\
**Created:** [July 19, 2024, 8:21pm UTC](https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364 "2024-07-19T20:21:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff8JF](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jeff8jf/32/4259_2.png) [@Jeff8JF](https://forum.ansible.com/u/Jeff8JF)\
**Post date:** [July 19, 2024, 8:21pm UTC](https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364/1 "2024-07-19T20:21:53Z")

</div>

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!

```auto
- 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

```

---

<div class="post-metadata">

**Author:** ![ads](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ads/32/747_2.png) [@ads](https://forum.ansible.com/u/ads)\
**Post date:** [July 19, 2024, 9:02pm UTC](https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364/2 "2024-07-19T21:02:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeff8JF](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jeff8jf/32/4259_2.png) [@Jeff8JF](https://forum.ansible.com/u/Jeff8JF)\
**Post date:** [July 19, 2024, 9:10pm UTC](https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364/3 "2024-07-19T21:10:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ads](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ads/32/747_2.png) [@ads](https://forum.ansible.com/u/ads)\
**Post date:** [July 19, 2024, 10:41pm UTC](https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364/4 "2024-07-19T22:41:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeff8JF](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jeff8jf/32/4259_2.png) [@Jeff8JF](https://forum.ansible.com/u/Jeff8JF)\
**Post date:** [July 22, 2024, 1:06pm UTC](https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364/5 "2024-07-22T13:06:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeff8JF](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jeff8jf/32/4259_2.png) [@Jeff8JF](https://forum.ansible.com/u/Jeff8JF)\
**Post date:** [July 22, 2024, 1:45pm UTC](https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364/6 "2024-07-22T13:45:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 21, 2024, 1:46pm UTC](https://forum.ansible.com/t/login-to-postgresql-to-create-a-new-database/7364/7 "2024-08-21T13:46:04Z")

</div>

Closed on August 21, 2024, 1:46pm UTC

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