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 Must Do Coding Questions - Topic-wise S shaurya uppal Follow Improve Article Tags : Technical Scripter Python python-modules Practice Tags : python Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like