xxxxxxxxxx
conn = psycopg2.connect("dbname=test user=postgres password=secret")
connect psycopg2 with python
xxxxxxxxxx
def connect_db_postgre(database, user, password, host, port="5432"):
"""Return connection and cursor from database
Args:
database (str): database
user (str): user
password (str): password
host (str): host
port (str): port, optional
"""
try:
conn = psycopg2.connect(database=database, user=user, password=password, host=host, port=port)
cursor = conn.cursor()
return conn, cursor
except Exception as e:
print(f'Erro: {str(e)}')
One free PostgreSQL database here: https://www.elephantsql.com/
pip install psycopg2