xxxxxxxxxx
Firstly, you need to install libpq-dev and python-dev ( python-dev is optional ).
Then, you can run pip install psycopg2 this command. Just run this command to install the above two mentioned packages.
If you face Python.h: No such file or directory this error then just install python3-dev.
xxxxxxxxxx
export PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"
pip install psycopg2