Python | Getting started with psycopg2-PostGreSQL Last Updated : 20 Feb, 2019 Improve Improve Like Article Like Save Share Report PostgreSQL is a powerful, open source object-relational database system. PostgreSQL runs on all major operating systems. PostgreSQL follows ACID property of DataBase system and has the support of triggers, updatable views and materialized views, foreign keys. To connect PostgreSQL we use psycopg2 . It is the best and most friendly Database adapter in python language. It is both Unicode and Python3 friendly. Installation Required – pip install psycopg2 Let’s get started and understand PostgreSQL connection in parts. Step #1: Connection to PostGreSQL import psycopg2 conn = psycopg2.connect(database ="gfgdb", user = "gfguser", password = "passgeeks", host = "52.33.0.1", port = "5432") print("Connection Successful to PostgreSQL") Step #2: Declare Cursor Allows Python code to execute PostgreSQL command in a database session. cur = conn.cursor() Step #3: Write your SQL Query and execute it. query = """select name, email from geeks_members;""" cur.execute(query) rows = cur.fetchall() # Now 'rows' has all data for x in rows: print(x[0], x[1]) Step #4: Close the connection conn.close() print('Connection closed') Like Article Suggest improvement Previous FreeGames module in Python Next NLP | Categorized Text Corpus Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads Do loop in Postgresql Using Psycopg2 Python Insert Python Dictionary in PostgreSQL using Psycopg2 Python PostgreSQL Connection Pooling Using Psycopg2 Get column names from PostgreSQL table using Psycopg2 Python Psycopg2 - Getting ID of row just inserted PostgreSQL - Connect To PostgreSQL Database Server in Python Python psycopg2 - Read Image How to Install psycopg2 Binary Module in Python ? Executing SQL query with Psycopg2 in Python Python Psycopg2 - Concatenate columns to new column Like shaurya uppal Follow Article Tags : python-modules Python Practice Tags : python