Failed to import the required Python library (psycopg2)

Failed to import the required Python library (psycopg2)

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

- 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

    "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"

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

2 Likes

Worked!
Thank you Ton.