DEV Community

Loading JSON into Postgres

Copple on April 16, 2020

Today I had to load some JSON data into Postgres. Postgres' COPY command it expects one JSON object per line rather than a full array. For examp...
Collapse
 
affluentbilbyclassifieds profile image
affluent-bilby-classifieds • Edited

Thanks for this Copple. I have signed up my project to the Supabase.io alpha program. I followed the instructions here. Can I please have the additional instructions to import the file into the "CREATE TABLE" page in Supabase? Thank you.

Collapse
 
kiwicopple profile image
Copple

Hey! This is actually specific to Postgres, not the Supabase interface. We are building easy ways to load data in Supabase, but they aren't finished yet.

You can use the method i describe in this article to load directly into the Postgres database we give you - you will need to connect to it directly using psql. Do you have psql on your computer?

Collapse
 
affluentbilbyclassifieds profile image
affluent-bilby-classifieds

Hi Copple.

Yes, I am running postgres (psql 9.6.18) in a docker container. I have successfully followed your instructions. It was after that I read about supabase and said Eureka!

I was going to try importing it today. I exported it first.
I used: "pg_dump -d menudb -U fruty -t menu > file.sql" but I haven't tried importing that into supabase as yet. I can put the contents in a codepen if you like.

I am very interested in directly importing it to the supabase Postgres database if you can advise how I should proceed.

Thank you

Thread Thread
 
kiwicopple profile image
Copple

You can import it exactly as you do it on the local machine! The key difference is that you need to change the connection parameters. So instead of the local host:

psql -h localhost -p 5432 postgres -U postgres

You use the connection details for the Supabase database

PGPASSWORD=[your password] psql -h XXX.supabase.co -p 5432 postgres -U postgres 

You can find the connection details in the "settings" page of your project in Supabase

Thread Thread
 
affluentbilbyclassifieds profile image
affluent-bilby-classifieds

That's fantastic! Thanks Copple. I will do that tomorrow morning.

Thread Thread
 
affluentbilbyclassifieds profile image
affluent-bilby-classifieds

psql -h XXXX.supabase.co -p 5432 postgres -U postgres -c "CREATE TABLE menudb (data jsonb);"
Password for user postgres:
CREATE TABLE
root@45279c35af82:/init/json# cat output.json | psql -h XXXX.supabase.co -p 5432 postgres -U postgres -c "COPY menudb (data) FROM STDIN;"
Password for user postgres:
COPY 34