-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_to_postgre.py
43 lines (37 loc) · 1.09 KB
/
json_to_postgre.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
import psycopg2
import json
# PostgreSQL connection details
DB_HOST = "<database-host>"
DB_PORT = "<database-port>"
DB_NAME = "<database-name>"
DB_USER = "<database-username>"
DB_PASSWORD = "<database-password>"
TABLE_NAME = "<table-name>"
# Read the JSON data from the file
with open('./data.json') as f:
data = json.load(f)
# Connect to PostgreSQL
conn = psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
dbname=DB_NAME,
user=DB_USER,
password=DB_PASSWORD
)
# Loop through the JSON data and insert each record into the PostgreSQL table
for item in data:
# Replace the keys below with the keys in your JSON data
name = item['name']
title = item['title']
subtitle = item['subtitle']
created_at = item['created_at']
# Insert the record into the PostgreSQL table
with conn.cursor() as cursor:
cursor.execute("""
INSERT INTO {} (name, title, subtitle, created_at)
VALUES (%s, %s, %s, %s)
RETURNING id
""".format(TABLE_NAME), (name, title, subtitle, created_at))
conn.commit()
conn.close()