{"id":100030,"date":"2021-02-17T11:00:00","date_gmt":"2021-02-17T09:00:00","guid":{"rendered":"https:\/\/examples.javacodegeeks.com\/?p=100030"},"modified":"2021-02-09T16:07:51","modified_gmt":"2021-02-09T14:07:51","slug":"python-postgresql-tutorial-using-psycopg2","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/","title":{"rendered":"Python PostgreSQL Tutorial Using Psycopg2"},"content":{"rendered":"<p>Hello in this tutorial, we will understand how to connect to the postgresql database via python programming.<\/p>\n<h2>1. Introduction<\/h2>\n<p>To connect with postgresql in python programming we have the following modules that are commonly available and known to the developer world as all these modules adhere to the <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0249\/\" target=\"_blank\" rel=\"noopener\">python database api specification:<\/a><\/p>\n<ul>\n<li>Psycopg2<\/li>\n<li>py-postgresql<\/li>\n<li>ocpgdb<\/li>\n<li>PyGreSQL<\/li>\n<li>pg8000<\/li>\n<\/ul>\n<h3>1.1 Setting up Python<\/h3>\n<p>If someone needs to go through the Python installation on Windows, please watch <a href=\"https:\/\/www.youtube.com\/watch?v=i-MuSAwgwCU\" target=\"_blank\" rel=\"noopener\">this<\/a> link. You can download the Python from <a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">this<\/a> link.<\/p>\n<h3>1.2 Setting up Psycopg2<\/h3>\n<p>Once the python is successfully installed on your system you can install the <strong>psycopg2<\/strong> using a simple <code>pip<\/code> command. You can fire the below command from the command prompt and it will successfully download the module from <a href=\"https:\/\/pypi.org\/project\/psycopg2\/\" target=\"_blank\" rel=\"noopener\">pypi.org<\/a> and install it.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Installation command<\/em><\/span><\/p>\n<pre class=\"brush:python;\">pip install psycopg2\n<\/pre>\n<h3>1.3 Setting up Postgres database<\/h3>\n<p>To start with the tutorial, I am hoping that you have the Postgres up and running in your localhost environment. For easy setup, I have the database up and running on the docker environment. You can execute the below command to get the container running on docker in minutes.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Docker command<\/em><\/span><\/p>\n<pre class=\"brush:plain; wrap-lines:false;\">docker run -d -p 5433:5432 -e POSTGRES_PASSWORD=password --name postgres postgres\n<\/pre>\n<p>If everything goes well the container would be started successfully as shown in Fig. 1. You can use the <code>docker ps -a<\/code> command to confirm that both the containers are started successfully. For further information on docker basics, you can navigate to this <a href=\"https:\/\/examples.javacodegeeks.com\/docker-basic-commands\/\" target=\"_blank\" rel=\"noopener\">tutorial<\/a>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1.jpg\"><img decoding=\"async\" width=\"818\" height=\"112\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1.jpg\" alt=\"python postgresql - on docker\" class=\"wp-image-100031\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1.jpg 818w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1-300x41.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1-768x105.jpg 768w\" sizes=\"(max-width: 818px) 100vw, 818px\" \/><\/a><figcaption>Fig. 1: Postgres container on Docker<\/figcaption><\/figure>\n<\/div>\n<h2>2. Python PostgreSQL Tutorial Using Psycopg2<\/h2>\n<p>Before going any deeper in the practical let me walk you through a simple architecture diagram where it shows that wherein the psycopg2 module fits in the picture.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart.jpeg\"><img decoding=\"async\" width=\"818\" height=\"210\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart.jpeg\" alt=\"python postgresql connection\" class=\"wp-image-100032\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart.jpeg 818w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart-300x77.jpeg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart-768x197.jpeg 768w\" sizes=\"(max-width: 818px) 100vw, 818px\" \/><\/a><figcaption>Fig. 2: Python postgresql connection<\/figcaption><\/figure>\n<\/div>\n<p>For the psycopg2 module to work we will supply the following attributes to the postgresql from the python application i.e.<\/p>\n<ul>\n<li><strong>user<\/strong> \u2013 Identity user to work with the postgresql database. Default postgresql user is <code>postgres<\/code><\/li>\n<li><strong>password<\/strong> \u2013 Credential of the identity user<\/li>\n<li><strong>host<\/strong> \u2013 Address of the database server. If running on localhost then you can either use localhost or 127.0.0.1<\/li>\n<li><strong>port<\/strong> &#8211; The port number. Default postgresql port is <code>5432<\/code><\/li>\n<li><strong>database<\/strong> \u2013 Database to which you want to connect. It can be left blank<\/li>\n<\/ul>\n<p>Let us dive in with the programming stuff now.<\/p>\n<h3>2.1 Creating a configuration file<\/h3>\n<p>Add the following code to the environment file wherein we will specify the connection and database details. You are free to change these details as per your configuration setup.<\/p>\n<p><span style=\"text-decoration: underline\"><em>local.env<\/em><\/span><\/p>\n<pre class=\"brush:plain;\">[DB]\nusername = postgres\npassword = password\nhost = localhost\nport = 5433\ndatabase =\n<\/pre>\n<h3>2.2 Creating a python script to read the configuration<\/h3>\n<p>Add the following code to the python script which will read the configuration file created above and return the config object to be used later while connecting to the database. The script will import the <code>configparser<\/code> module for reading the configuration file. Remember to give the correct path where the <code>local.env<\/code> is created.<\/p>\n<p><span style=\"text-decoration: underline\"><em>readdbconfig.py<\/em><\/span><\/p>\n<pre class=\"brush:python;\">import configparser\ndef read_db_params():\n    # reading the env file\n    config = configparser.ConfigParser()\n    config.read('config\/local.env')\n    return config\n<\/pre>\n<h3>2.3 Connecting to the database<\/h3>\n<p>Add the following code to the python script which will connect to the postgresql with the help of the <code>psycopg2<\/code> module. If the connection is successful the script will print the database version and if not will throw an error and print the exception message on the console. The python script consists of \u2013<\/p>\n<ul>\n<li><code>connect()<\/code> method for creating a connection to the postgresql<\/li>\n<li><code>cursor()<\/code> method to interact with the database and perform operations<\/li>\n<li><code>execute()<\/code> method to run a database query<\/li>\n<li><code>fetchone()<\/code> method to return a single row<\/li>\n<li><code>close()<\/code> method to close the cursor and connector object once the work gets done and avoid connection leak issues<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline\"><em>connecttodb.py<\/em><\/span><\/p>\n<pre class=\"brush:python;\"># python postgresql\nimport psycopg2\nfrom psycopg2 import Error\nfrom readdbconfig import *\ndef connect(self):\n    try:\n        # connect to database\n        # reading the database parameters from the config object\n        conn = psycopg2.connect(\n            user=self.get('DB', 'username'),\n            password=self.get('DB', 'password'),\n            host=self.get('DB', 'host'),\n            port=self.get('DB', 'port'),\n            database=self.get('DB', 'database')\n        )\n        print_version(conn)\n    except(Exception, Error) as error:\n        print(error)\ndef print_version(conn):\n    # creating a cursor to perform database operations\n    cursor = conn.cursor()\n    try:\n        # execute the sql query\n        cursor.execute('SELECT version();')\n        # fetch result\n        record = cursor.fetchone()\n        print('PostgreSQL version = {}'.format(record))\n    except(Exception, Error) as error:\n        print(error)\n    finally:\n        if conn is not None:\n            cursor.close()\n            conn.close()\n            print('\\nDatabase connection closed.')\n# driver code\nif __name__ == '__main__':\n    # method will read the env file and return the config object\n    # read_db_params() method is coming from the readdbconfig.py file\n    db_params = read_db_params()\n    # config object is passed to the connect\n    connect(db_params)\n<\/pre>\n<p>If everything goes well the following output will be shown in the IDE console. If not the exception message will be shown.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Success logs<\/em><\/span><\/p>\n<pre class=\"brush:bash;\">PostgreSQL version = ('PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit',)\n\nDatabase connection closed.\n<\/pre>\n<p>That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!<\/p>\n<h2>3. Summary<\/h2>\n<p>In this tutorial, we learned:<\/p>\n<ul>\n<li>Introduction to psycopg2 module<\/li>\n<li>Sample program to connect to the database with the help of psycopg2 module<\/li>\n<\/ul>\n<p>You can download the source code of this tutorial from the <a href=\"#projectDownload\">Downloads<\/a> section.<\/p>\n<h2><a name=\"projectDownload\"><\/a>4. Download the Project<\/h2>\n<p>This was a python programming tutorial to connect to the database with the help of the psycopg2 module.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/Python-PostgreSQL-Tutorial-Using-Psycopg2.zip\"><strong>Python PostgreSQL Tutorial Using Psycopg2<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello in this tutorial, we will understand how to connect to the postgresql database via python programming. 1. Introduction To connect with postgresql in python programming we have the following modules that are commonly available and known to the developer world as all these modules adhere to the python database api specification: Psycopg2 py-postgresql ocpgdb &hellip;<\/p>\n","protected":false},"author":119,"featured_media":99891,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46689],"tags":[1716],"class_list":["post-100030","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python PostgreSQL Tutorial Using Psycopg2 - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Hello in this tutorial, we will understand how to connect to the postgresql database via python programming. 1. Introduction To connect with postgresql in\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python PostgreSQL Tutorial Using Psycopg2 - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Hello in this tutorial, we will understand how to connect to the postgresql database via python programming. 1. Introduction To connect with postgresql in\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-17T09:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\"},\"headline\":\"Python PostgreSQL Tutorial Using Psycopg2\",\"datePublished\":\"2021-02-17T09:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/\"},\"wordCount\":684,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/\",\"name\":\"Python PostgreSQL Tutorial Using Psycopg2 - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg\",\"datePublished\":\"2021-02-17T09:00:00+00:00\",\"description\":\"Hello in this tutorial, we will understand how to connect to the postgresql database via python programming. 1. Introduction To connect with postgresql in\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"set python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Web Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/web-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/web-development\/python\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python PostgreSQL Tutorial Using Psycopg2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\",\"name\":\"Yatin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg\",\"caption\":\"Yatin\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python PostgreSQL Tutorial Using Psycopg2 - Java Code Geeks","description":"Hello in this tutorial, we will understand how to connect to the postgresql database via python programming. 1. Introduction To connect with postgresql in","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/","og_locale":"en_US","og_type":"article","og_title":"Python PostgreSQL Tutorial Using Psycopg2 - Java Code Geeks","og_description":"Hello in this tutorial, we will understand how to connect to the postgresql database via python programming. 1. Introduction To connect with postgresql in","og_url":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2021-02-17T09:00:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg","type":"image\/jpeg"}],"author":"Yatin","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"Python PostgreSQL Tutorial Using Psycopg2","datePublished":"2021-02-17T09:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/"},"wordCount":684,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg","keywords":["python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/","url":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/","name":"Python PostgreSQL Tutorial Using Psycopg2 - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg","datePublished":"2021-02-17T09:00:00+00:00","description":"Hello in this tutorial, we will understand how to connect to the postgresql database via python programming. 1. Introduction To connect with postgresql in","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg","width":150,"height":150,"caption":"set python"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Web Development","item":"https:\/\/examples.javacodegeeks.com\/category\/web-development\/"},{"@type":"ListItem","position":3,"name":"Python","item":"https:\/\/examples.javacodegeeks.com\/category\/web-development\/python\/"},{"@type":"ListItem","position":4,"name":"Python PostgreSQL Tutorial Using Psycopg2"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2023\/09\/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/100030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=100030"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/100030\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/99891"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=100030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=100030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=100030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}