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

Thread Thread
 
affluentbilbyclassifieds profile image
affluent-bilby-classifieds • Edited

Looks like it's worked!

Thank you!

Thread Thread
 
kiwicopple profile image
Copple

Woohoo! I'm impressed you pursued this all the way to the end. If you need help with anything else, feel free to reach my cofounder and I on alpha@supabase.io. Would love to hear more about what you're building

Thread Thread
 
affluentbilbyclassifieds profile image
affluent-bilby-classifieds • Edited

The next thing I need to find out is how to use it as a datasource (restful API I think it's called) for react-admin. Is there a similar page to the "connection details" that gives me the path information I need to enter into my react-admin code related to data provider (currently using ra-data-simple-rest) configuration?

Admin dataProvider={simpleRestProvider('http://path.to.my.api/')

edit: I just saw your response.

should I remove my next question and e-mail instead?

Thread Thread
 
kiwicopple profile image
Copple

I've never used React Admin, so I don't know exactly how it would work, but I imagine you can go to the "Docs" section in your dashboard and use the "Bash" commands to "cURL" the data 😂. What a mouthful

Alternatively, we have a table view, which is getting better every week. (go to the "Zap" icon, then toggle at the top between Table/SQL). We have some huge improvements coming this week, and eventually you won't even need React Admin - we are building it for you :). If you've ever used Airtable, this is how our Table View will function

Thread Thread
 
affluentbilbyclassifieds profile image
affluent-bilby-classifieds

Thanks Copple.

If I can "cURL" I can probably use axios or fetch for CRUD. I will check out the docs and link this thread to our dev.
We do intend to use React-Admin and it will become a dashboard for the business.