{"id":100337,"date":"2021-03-01T11:00:00","date_gmt":"2021-03-01T09:00:00","guid":{"rendered":"https:\/\/examples.javacodegeeks.com\/?p=100337"},"modified":"2021-02-22T22:03:40","modified_gmt":"2021-02-22T20:03:40","slug":"python-postgresql-crud-operations-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/","title":{"rendered":"Python PostgreSQL CRUD Operations Example"},"content":{"rendered":"<p>Hello in this tutorial, we will understand how to perform SQL &#8211; Crud operations 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> &#8211;<\/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 container is 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-1.jpg\"><img decoding=\"async\" width=\"818\" height=\"112\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1-1.jpg\" alt=\"Python PostgreSQL CRUD - container on docker\" class=\"wp-image-100338\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1-1.jpg 818w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1-1-300x41.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/docker-postgres-container-img1-1-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 CRUD Operations Example<\/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.<\/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-1.jpeg\"><img decoding=\"async\" width=\"818\" height=\"210\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart-1.jpeg\" alt=\"Python PostgreSQL CRUD - connection\" class=\"wp-image-100339\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart-1.jpeg 818w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart-1-300x77.jpeg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-psycopg2-postgres-application-flowchart-1-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><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:python;\"># python postgresql\nimport psycopg2\nfrom psycopg2 import Error\nfrom readdbconfig import *\ndef connect(database_name):\n    try:\n        # method will read the env file and return the config object\n        params = read_db_params()\n        db_name = params.get('DB', 'database') if not database_name else database_name\n        # connect to database\n        # reading the database parameters from the config object\n        conn = psycopg2.connect(\n            user=params.get('DB', 'username'),\n            password=params.get('DB', 'password'),\n            host=params.get('DB', 'host'),\n            port=params.get('DB', 'port'),\n            database=db_name\n        )\n        return 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    # ops method\n    print_version(connect(''))\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>Logs<\/em><\/span><\/p>\n<pre class=\"brush:plain;\">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<h3>2.4 Create table<\/h3>\n<p>Add the following code to the python script which will <span style=\"text-decoration: underline;\">create a table<\/span> in the <code>employee<\/code> database. The python script will create a table if it does not exists. If it does the script will throw an error. <em>Ensure that the database already exists<\/em>.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>createtable.py<\/em><\/span><\/p>\n<pre class=\"brush:python;\">from connecttodb import *\n# create table method\ndef create_table(conn):\n    # creating a cursor to perform a sql operation\n    cursor = conn.cursor()\n    # sql query\n    query = '''\n    CREATE TABLE employee (id INT NOT NULL, first_name VARCHAR(255), \n    last_name VARCHAR(100), email VARCHAR(150), \n    gender VARCHAR(50), phone VARCHAR(100), \n    PRIMARY KEY (id));\n    '''\n    try:\n        # execute the command\n        cursor.execute(query)\n        # commit the changes\n        conn.commit()\n        print('Table created successfully')\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    # connect to database and create table\n    create_table(connect('employee'))\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>Logs<\/em><\/span><\/p>\n<pre class=\"brush:plain;\">Table created successfully\n\nDatabase connection closed.\n<\/pre>\n<h3>2.5 Insert data into the table<\/h3>\n<p>Add the following code to the python script which will <span style=\"text-decoration: underline;\">insert the default employee-related data<\/span> to the <code>employee<\/code> table. The python script will check if default data is present in the table or not. If <em>present<\/em> it will skip the insert operation. If <em>not<\/em> the default data will be inserted.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>insertdata.py<\/em><\/span><\/p>\n<pre class=\"brush:python;\">from connecttodb import *\nfrom helper import *\n# insert data\ndef insert(conn):\n    # creating a cursor to perform a sql operation\n    cursor = conn.cursor()\n    # sql query\n    query = '''\n    INSERT INTO employee (id, first_name, last_name, email, gender, phone) VALUES (%s, %s, %s, %s, %s, %s);\n    '''\n    try:\n        count = get_records_count(cursor)\n        if count &gt; 0:\n            print('Default data present. Skipping insert')\n        else:\n            data = [\n                (1, 'Marga', 'Cronchey', 'mcronchey0@pen.io', 'F', '314-289-7265'),\n                (2, 'Theda', 'Mushrow', 'tmushrow1@whitehouse.gov', 'F', '804-163-9834'),\n                (3, 'Marielle', 'Bonicelli', 'mbonicelli2@sitemeter.com', 'F', '624-922-2416'),\n                (4, 'Locke', 'Watkinson', 'lwatkinson3@accuweather.com', 'M', '456-260-1052'),\n                (5, 'Blakelee', 'Wilcot', 'bwilcot4@twitpic.com', 'M', '608-344-4090')\n            ]\n            # execute the command\n            cursor.executemany(query, data)\n            # commit the changes\n            conn.commit()\n            print('{} records inserted'.format(cursor.rowcount))\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    # connect to database and insert data into the table\n    insert(connect('employee'))\n<\/pre>\n<p>If everything goes well the following output will be shown in the IDE console.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Logs<\/em><\/span><\/p>\n<pre class=\"brush:plain;\">5 records inserted\n\nDatabase connection closed.\n<\/pre>\n<h3>2.6 Get all data from the table<\/h3>\n<p>Add the following code to the python script which will <span style=\"text-decoration: underline;\">get all the employee-related data<\/span> from the <code>employee<\/code> table. The python script will check if any existing data is present in the table or not. If <em>present<\/em> it will print the data on the console. If <em>not<\/em> the following message &#8211; <code>No data present in db<\/code> will be printed on the console.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>getall.py<\/em><\/span><\/p>\n<pre class=\"brush:python;\">from connecttodb import *\nfrom helper import *\n# get all records\ndef get_all(conn):\n    # creating a cursor to perform a sql operation\n    cursor = conn.cursor()\n    # sql query\n    query = '''SELECT * FROM employee;'''\n    try:\n        count = get_records_count(cursor)\n        if count == 0:\n            print('No data present in db')\n        else:\n            # execute the command\n            cursor.execute(query)\n            records = cursor.fetchall()\n            print('EMPLOYEE INFORMATION')\n            print('-------------------------------------')\n            for record in records:\n                full_name = record[1] + \" \" + record[2]\n                print('Id = {}, Name = {}, Email = {}, Gender = {}, Phone = {}'.format(record[0], full_name,\n                                                                                       record[3], record[4], record[5]))\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    # connect to database and get all data\n    get_all(connect('employee'))\n<\/pre>\n<p>If everything goes well the following output will be shown in the IDE console.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Logs<\/em><\/span><\/p>\n<pre class=\"brush:plain;\">EMPLOYEE INFORMATION\n-------------------------------------\nId = 1, Name = Marga Cronchey, Email = mcronchey0@pen.io, Gender = F, Phone = 314-289-7265\nId = 2, Name = Theda Mushrow, Email = tmushrow1@whitehouse.gov, Gender = F, Phone = 804-163-9834\nId = 3, Name = Marielle Bonicelli, Email = mbonicelli2@sitemeter.com, Gender = F, Phone = 624-922-2416\nId = 4, Name = Locke Watkinson, Email = lwatkinson3@accuweather.com, Gender = M, Phone = 456-260-1052\nId = 5, Name = Blakelee Wilcot, Email = bwilcot4@twitpic.com, Gender = M, Phone = 608-344-4090\n\nDatabase connection closed.\n<\/pre>\n<h3>2.7 Update a record in the table<\/h3>\n<p>Add the following code to the python script which will <span style=\"text-decoration: underline;\">update an employee record<\/span> into the table. The python script will check if the employee is present in the database. If <em>present<\/em> the record will be updated. If <em>not<\/em> it will print the following message &#8211; <code>Employee id = 5 not found<\/code> will be printed on the console.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>update.py<\/em><\/span><\/p>\n<pre class=\"brush:python;\">from connecttodb import *\nfrom helper import *\n# update a record\ndef update(conn, eid):\n    # creating a cursor to perform a sql operation\n    cursor = conn.cursor()\n    # sql query\n    query = '''UPDATE employee SET gender = %s WHERE id = %s;'''\n    try:\n        record = get_by_id(cursor, eid)\n        if record is None:\n            print('Employee id = {} not found'.format(eid))\n        else:\n            # execute the command\n            cursor.execute(query, ['F', eid])\n            # commit the changes\n            conn.commit()\n            print('Employee id = {} updated successfully'.format(eid))\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    # connect to database and update a record\n    update(connect('employee'), 5)\n<\/pre>\n<p>If everything goes well the following output will be shown in the IDE console. You can run the <code>getall.py<\/code> python script to fetch the records.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Logs<\/em><\/span><\/p>\n<pre class=\"brush:plain;\">Employee id = 5 updated successfully\n\nDatabase connection closed.\n<\/pre>\n<h3>2.8 Delete a record in the table<\/h3>\n<p>Add the following code to the python script which will <span style=\"text-decoration: underline;\">delete an employee record<\/span> into the table. The python script will check if the employee is present in the database. If <em>present<\/em> the record will be deleted. If <em>not<\/em> it will print the following message &#8211; <code>Employee id = 5 not found<\/code> will be printed on the console.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>delete.py<\/em><\/span><\/p>\n<pre class=\"brush:python;\">from connecttodb import *\nfrom helper import *\n# delete a record\ndef delete(conn, eid):\n    # creating a cursor to perform a sql operation\n    cursor = conn.cursor()\n    # sql query\n    query = '''DELETE FROM employee WHERE id = %s;'''\n    try:\n        record = get_by_id(cursor, eid)\n        if record is None:\n            print('Employee id = {} not found'.format(eid))\n        else:\n            # execute the command\n            cursor.execute(query, [eid])\n            # commit the changes\n            conn.commit()\n            print('Employee id = {} deleted successfully'.format(eid))\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    # connect to database and delete a record\n    delete(connect('employee'), 5)\n<\/pre>\n<p>If everything goes well the following output will be shown in the IDE console. You can run the <code>getall.py<\/code> python script to fetch the records.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Logs<\/em><\/span><\/p>\n<pre class=\"brush:plain;\">Employee id = 5 deleted successfully\n\nDatabase connection closed.\n<\/pre>\n<p>Similarly, we can create other scripts like <code>deleteall.py<\/code> to delete all records from the <code>employee<\/code> database and many others. 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<li>Sample programs to perform CRUD operations<\/li>\n<\/ul>\n<p>Now, you can check our <a href=\"https:\/\/examples.javacodegeeks.com\/python-postgresql-tutorial-using-psycopg2\/\">Python PostgreSQL Tutorial Using Psycopg2<\/a>.<\/p>\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 PostgreSQL database with the help of the psycopg2 module and perform CRUD operations.<\/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-CRUD-Operations-Example.zip\"><strong>Python PostgreSQL CRUD Operations Example<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello in this tutorial, we will understand how to perform SQL &#8211; Crud operations 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 &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":[1720,719,1716],"class_list":["post-100337","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-crud","tag-postgresql","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 CRUD Operations Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Hello in this tutorial, we will understand how to perform SQL - Crud operations to the PostgreSQL database via python programming. 1. Introduction To\" \/>\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-crud-operations-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python PostgreSQL CRUD Operations Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Hello in this tutorial, we will understand how to perform SQL - Crud operations to the PostgreSQL database via python programming. 1. Introduction To\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/\" \/>\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-03-01T09: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=\"10 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-crud-operations-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/\"},\"author\":{\"name\":\"Yatin\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13\"},\"headline\":\"Python PostgreSQL CRUD Operations Example\",\"datePublished\":\"2021-03-01T09:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/\"},\"wordCount\":1114,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg\",\"keywords\":[\"crud\",\"PostgreSQL\",\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/\",\"name\":\"Python PostgreSQL CRUD Operations Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg\",\"datePublished\":\"2021-03-01T09:00:00+00:00\",\"description\":\"Hello in this tutorial, we will understand how to perform SQL - Crud operations to the PostgreSQL database via python programming. 1. Introduction To\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#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-crud-operations-example\/#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 CRUD Operations Example\"}]},{\"@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 CRUD Operations Example - Java Code Geeks","description":"Hello in this tutorial, we will understand how to perform SQL - Crud operations to the PostgreSQL database via python programming. 1. Introduction To","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-crud-operations-example\/","og_locale":"en_US","og_type":"article","og_title":"Python PostgreSQL CRUD Operations Example - Java Code Geeks","og_description":"Hello in this tutorial, we will understand how to perform SQL - Crud operations to the PostgreSQL database via python programming. 1. Introduction To","og_url":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2021-03-01T09: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":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/"},"author":{"name":"Yatin","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/9874407a37b028e8be3276e2b5960d13"},"headline":"Python PostgreSQL CRUD Operations Example","datePublished":"2021-03-01T09:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/"},"wordCount":1114,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg","keywords":["crud","PostgreSQL","python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/","url":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/","name":"Python PostgreSQL CRUD Operations Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2021\/02\/python-logo.jpg","datePublished":"2021-03-01T09:00:00+00:00","description":"Hello in this tutorial, we will understand how to perform SQL - Crud operations to the PostgreSQL database via python programming. 1. Introduction To","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/python-postgresql-crud-operations-example\/#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-crud-operations-example\/#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 CRUD Operations Example"}]},{"@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\/100337","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=100337"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/100337\/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=100337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=100337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=100337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}