Skip to content
Advertisement

How to import a JSON file into postgresql databse?

I just tried to read a .JSON file in my PostgreSQL database but it is not able to read it. I did it with a .csv file, but with a .JSON file, I am not able to do it.

This is how the .JSON file looks like:

This is the code that I tried, I created the table first and then copy the data from the file into the database.

Advertisement

Answer

You can import this json file of yours to a temporary table and from there populate the table notifies. For example:

Create a tmp table ..

.. import your json file into the table tmp using COPY ..

… and finally populate the table notifies:

After that you may want to drop the table tmp

EDIT: A quite elegant alternative is to use json_populate_record, as suggested by @Jeremy. Thanks! See comments below.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement