I am trying to set up a PostgreSQL database for my django project, which I believe I have done now thanks to the replies to my last question Problems setting up a postgreSQL
I had such problem when trying to run python script as a sudo, while psycopg2 was installed via pip3 to my own user's directory.
I managed to resolve the issue for myself removing pip3 version, and just installing it via apt:
pip3 uninstall psycopg2 sudo apt install python3-psycopg2