Make a JSON from PostgreSQL table (for Django)

First, choose a folder you want to copy the table to and grant postgres user access to it. I chose /home:

sudo chown -R postgres:postgres /home

Then log into postgres user:

sudo su - postgres

Then get to your database. If it is a Django project, you can find the name in settings.py:

psql dbname

Then, if you need your app’s tables, make a table name from these components:

{{ app_name }}_{{ model_name }}

For app userapp and model User, I have userapp_user. So in a /home/users.json I want to copy:

\t
\a
\o /home/users.json
SELECT row_to_json(r) FROM userapp_user AS r;

That’s it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.