# Unable to connect to postgres using postgesql\_db ansible module

**URL:** <https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695>\
**Category:** Get Help\
**Tags:** ansible, postgresql\
**Created:** [June 21, 2024, 4:57am UTC](https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695 "2024-06-21T04:57:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukesh1328](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@Mukesh1328](https://forum.ansible.com/u/Mukesh1328)\
**Post date:** [June 21, 2024, 4:57am UTC](https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695/1 "2024-06-21T04:57:14Z")

</div>

Here is the playbook im using

```auto
- hosts: all
  become: true
  tasks:
    - name: create db
      become_user: postgres
      postgresql_db:
        name: test_db

```

Output after executing the play

```auto
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_postgresql_db_payload_fwv6hxn3/ansible_postgresql_db_payload.zip/ansible/modules/database/postgresql/postgresql_db.py", line 541, in main
  File "/usr/lib64/python3.6/site-packages/psycopg2/ __init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?

fatal: [***********************.net]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "ca_cert": null,
            "conn_limit": "",
            "db": "test_db",
            "encoding": "UTF8",
            "lc_collate": "en_US.UTF-8",
            "lc_ctype": "en_US.UTF-8",
            "login_host": "",
            "login_password": "",
            "login_unix_socket": "",
            "login_user": "postgres",
            "maintenance_db": "postgres",
            "name": "test_db",
            "owner": "",
            "port": 5432,
            "session_role": null,
            "ssl_mode": "prefer",
            "state": "present",
            "tablespace": "",
            "target": "",
            "target_opts": "",
            "template": ""
        }
    },
    "msg": "unable to connect to database: connection to server on socket \"/var/run/postgresql/.s.PGSQL.5432\" failed: No such file or directory\n\tIs the server running locally and accepting connections on that socket?\n"
}

```

Note: python3-psycopg2 is already installed in managed nodes.

Ansible 2.9.27  
Postgres version: 15

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [June 21, 2024, 11:10am UTC](https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695/2 "2024-06-21T11:10:09Z")

</div>

Check the PostgreSQL socket path?

> [@Mukesh1328](#):
>
> ```auto
> connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
> Is the server running locally and accepting connections on that socket?
> 
> ```

---

<div class="post-metadata">

**Author:** ![Andersson007](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/andersson007/32/89_2.png) [@Andersson007](https://forum.ansible.com/u/Andersson007)\
**Post date:** [June 21, 2024, 11:55am UTC](https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695/3 "2024-06-21T11:55:19Z")

</div>

Hey folks,

> [@Mukesh1328](#):
>
> Ansible 2.9.27

@Mukesh1328 It’s very old, could you upgrade Ansible or install the latest collection version using the `ansible-galaxy` command and try again if the socket investigation won’t help?

@chris your help is much appreciated in all of those topics, thanks for the contribution!

---

<div class="post-metadata">

**Author:** ![Mukesh1328](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@Mukesh1328](https://forum.ansible.com/u/Mukesh1328)\
**Post date:** [June 21, 2024, 1:22pm UTC](https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695/4 "2024-06-21T13:22:07Z")

</div>

> [@chris](#):
>
> ck the PostgreSQL socket path?

In the managed node it shows  
`/tmp/.s.PGSQL.5432`

How can we tell ansible to pick this path?

---

<div class="post-metadata">

**Author:** ![Mukesh1328](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@Mukesh1328](https://forum.ansible.com/u/Mukesh1328)\
**Post date:** [June 21, 2024, 1:23pm UTC](https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695/5 "2024-06-21T13:23:00Z")

</div>

Sure, We are planning to do the upgrade. thanks you for response @Andersson007

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [June 22, 2024, 9:28am UTC](https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695/6 "2024-06-22T09:28:00Z")

</div>

Perhaps you would need to write a task to find the socket path, however I’d suggest upgrading Ansible first — hopefully this won’t be needed.

---

<div class="post-metadata">

**Author:** ![johan](https://avatars.discourse-cdn.com/v4/letter/j/dec6dc/32.png) [@johan](https://forum.ansible.com/u/johan)\
**Post date:** [June 22, 2024, 1:34pm UTC](https://forum.ansible.com/t/unable-to-connect-to-postgres-using-postgesql-db-ansible-module/6695/7 "2024-06-22T13:34:04Z")

</div>

You should check the postgresql collection version you are using too.

The current one has a ‘login\_unix\_socket’ keyword to specify a socket, this is perhaps what you are looking for :

[https://docs.ansible.com/ansible/latest/collections/community/postgresql/postgresql\_query\_module.html](https://docs.ansible.com/ansible/latest/collections/community/postgresql/postgresql_query_module.html)
