- Install Psycopg2 package in Python
pip install psycopg2 or
pipenv install psycopg2 (virtual python environment)
- Install postgresql in Ubuntu
sudo apt update
sudo apt install postgresql postgresql-contrib
- After successfully install postgresql, the default role is postgres. So when you connect postgresql service in your own Ubuntu user, you cannot connect it, reporting the username not in the role. Thus create a role using your own username, and add your username in ROLE of postgres
psql postgres
postgres=# CREATE ROLE ubuntu_username superuser;
postgres=# ALTER ROLE ubuntu_username WITH LOGIN;
- In python, connect postgresql, e.g.
import psycopg2
conn = psycopg2.connect("dbname=test user=user_name")
Like this:
Like Loading...