Python | Getting started with psycopg2-PostGreSQL Last Updated : 20 Feb, 2019 Comments Improve Suggest changes Like Article Like 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 Python3 1== 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. Python3 1== cur = conn.cursor() Step #3: Write your SQL Query and execute it. Python3 1== 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 Python3 1== conn.close() print('Connection closed') Comment More infoAdvertise with us Next Article Python | Getting started with psycopg2-PostGreSQL S shaurya uppal Follow Improve Article Tags : Technical Scripter Python python-modules Practice Tags : python Similar Reads Do loop in Postgresql Using Psycopg2 Python In this article, we use psycopg2 to loop through all data points using psycopg2 function in Python. We will first connect our PostgreSQL database using psycopg2.connect method, and pass the connection parameters such as the host, database, user, and password. Then we will create a cursor using the c 4 min read Python PostgreSQL Connection Pooling Using Psycopg2 In this article, We will cover the basics of connection pooling using connection pooling in Python applications, and provide step-by-step instructions on how to implement connection pooling using Psycopg2. Whether you are building a small-scale application or a large-scale enterprise application, un 6 min read Python Select from PostgreSQL Table using Psycopg2 This article will introduce you to the use of the psycopg2 module which is used to connect to a PostgreSQL database from Python. We will look over how to establish a connection to a database, create a cursor object to execute DBMS SQL statements, execute a SELECT statement to retrieve the data from 7 min read Connecting PostgreSQL with SQLAlchemy in Python In this article, we will discuss how to connect PostgreSQL with SQLAlchemy in Python. In order to connect with any Database management system, it is essential to create an engine object, that serves as a central source of connection by providing a connection pool that manages the database connection 3 min read Executing SQL query with Psycopg2 in Python In this article, we are going to see how to execute SQL queries in PostgreSQL using Psycopg2 in Python. Psycopg2 is a PostgreSQL database driver, it is used to perform operations on PostgreSQL using python, it is designed for multi-threaded applications. SQL queries are executed with psycopg2 with t 2 min read Like