I am new to postgresql. I would like to insert information from .json and create a new table in Postgresql using python/psycopg2. I have looked over some StackOverflow posts and psychopg2 documentation without getting much further. The closest question is here, from which I derived the following:
The test .json file is as follows (which only has 1-level i.e. no nested .json structure):
[{"last_update": "2019-02-01"}]
Attempted python code:
import psycopg2
from psycopg2.extras import Json
from psycopg2 import Error
from unipath import Path
import io
def insert_into_table(json_data):
try:
with psycopg2.connect( user = "thisuser",
password = "somePassword",
host = "127.0.0.654165",
port = '5455',
database = "SqlTesting") as conn:
cursor = conn.cursor()
read_json = io.open(data_path, encoding='utf-8')
read_json_all = read_json.readlines()
query = "INSERT INTO new_table VALUES (%s)"
cursor.executemany(query, (read_json_all,))
conn.commit()
print("Json data import successful")
except (Exception, psycopg2.Error) as error:
print("Failed json import: {}".format(error))
insert_into_table(data_path)
The above code didn't work regardless whether new_table didn't exist or if it was created manually as a place-holder.
Rather, it produced the following error message:
Failed json import: relation "new_table" does not exist
LINE 1: INSERT INTO new_table VALUES ('[{"last_update": "2019-02-01"...
During debugging, I saw:
for i in read_json:
print (i)
# will result
# [{"last_update": "2019-02-01"}]
And
print (read_json_all)
# Will result
# ['[{"last_update": "2019-02-01"}]']