Bootstrap FreeKB - Postgres (SQL) - Update JSONB using Python
Postgres (SQL) - Update JSONB using Python

Updated:   |  Postgres (SQL) articles

The psycopg2 module can be used to connect to an Postgres database. pip install can be used to install the wheel and psycopg2-binary packages.

pip install wheel
pip install psycopg2-binary

 

And here is an example of how to connect to Postgres database in Python. This is just a super simple example, with no error handling.

#!/usr/bin/python3
import psycopg2
db_name = "mydb"
db_host = "10.11.12.13"
db_user = "john.doe"
db_pass = "itsasecret"
db_port = "5432"
connection = psycopg2.connect(database=db_name,
                        host=db_host,
                        user=db_user,
                        password=db_pass,
                        port=db_port)
connection.close()

 

Let's say you have a table that has a JSONB column.

                                            Table "public.mytable"
        Column         |           Type           | Collation | Nullable |                       Default                       
-----------------------+--------------------------+-----------+----------+-----------------------------------------------------
 id                    | integer                  |           | not null | 
 name                  | varchar(30)              |           |          | 
 json                  | jsonb                    |           |          | 

 

And here is an example of how to insert into the JSONB column with no error handling.

#!/usr/bin/python3
import psycopg2
from datetime import datetime
db_name = "mydb"
db_host = "10.11.12.13"
db_user = "john.doe"
db_pass = "itsasecret"
db_port = "5432"
connection = psycopg2.connect(database=db_name,
                        host=db_host,
                        user=db_user,
                        password=db_pass,
                        port=db_port)
cursor    = connection.cursor()
statement = cursor.execute("update mytable set json='{\"data\": [{\"date\": \"11/18/2024 @ 00:00:00 AM\", \"message\": \"test\"}]}' where id='1')
connection.commit()
cursor.close()
connection.close()

 

 Let's say you have a dictionary. You can use json.dumps to convert the dictionary to json.

#!/usr/bin/python3
import psycopg2
import json
from datetime import datetime

db_name = "mydb"
db_host = "10.11.12.13"
db_user = "john.doe"
db_pass = "itsasecret"
db_port = "5432"
connection = psycopg2.connect(database=db_name,
                        host=db_host,
                        user=db_user,
                        password=db_pass,
                        port=db_port)
cursor    = connection.cursor()

dictionary = { 'name': 'John Doe' }
json_dump = json.dumps(dictionary)

statement = f"update members set history='{json_dumps}' where id='{id}'"

connection.commit()
cursor.close()
connection.close()

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c0f192 in the box below so that we can be sure you are a human.