#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('reload_ext', 'sql') # %load_ext sql get_ipython().run_line_magic('sql', 'postgresql://postgres:postgres@localhost/postgres') # In[2]: get_ipython().run_line_magic('sql', 'select version(); #using %sql magic line') get_ipython().run_line_magic('sql', 'select * from notes;') # In[3]: import psycopg2 #using psycopg2 library """you can define the variables separately and then use only the variables names here like in this blog https://www.a2hosting.in/kb/developer-corner/postgresql/connecting-to-postgresql-using-python """ conn = psycopg2.connect(host="localhost", user="postgres", password="postgres", dbname="postgres") cur = conn.cursor() cur.execute("select * from notes") cur.fetchall() # In[4]: conn.close() # In[5]: cur.execute("select * from notes") cur.fetchall()