# Failed to import the required Python library (psycopg2)

**URL:** <https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/6649>\
**Category:** Get Help\
**Created:** [June 19, 2024, 4:37am UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/6649 "2024-06-19T04:37:06Z")\
**Posts on this page:** 3\
**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 19, 2024, 4:37am UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/6649/1 "2024-06-19T04:37:06Z")

</div>

Failed to import the required Python library (psycopg2)

Im using postgresql community module to create DB and user.  
Here is my simple play

```auto
- hosts: all
  become: true
  become_user: postgres
  tasks:

    - name: create db
      postgresql_db:
        name: test_db
        encoding: UTF8
        lc_collate: en_US.UTF-8
        lc_ctype: en_US.UTF-8

    - name: add user
      postgresql_user:
        db: test_db
        name: test_user
        password: mypassword
        priv: ALL
        state: present

```

I got a error message

```auto
    "msg": "Failed to import the required Python library (psycopg2) on scrbtrheldbaas002.crb.apmoller.net's Python /usr/bin/python3.6. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"

```

---

<div class="post-metadata">

**Author:** ![tonk](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tonk/32/214_2.png) [@tonk](https://forum.ansible.com/u/tonk)\
**Post date:** [June 19, 2024, 7:28am UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/6649/2 "2024-06-19T07:28:32Z")

</div>

On the target node you need to install the Python=\> PostgreSQL connector.  
Depending on the OS you can do that with Pip or the package manager.

On RHEL 9 the package is called `python3-psycopg2`

---

<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 19, 2024, 10:34am UTC](https://forum.ansible.com/t/failed-to-import-the-required-python-library-psycopg2/6649/3 "2024-06-19T10:34:12Z")

</div>

> [@tonk](#):
>
> python3-psycopg2

Worked!  
Thank you Ton.
