Meza: From PostgreSQL table to a JSON file

dixpac

Dino

Posted on November 17, 2019

Meza: From PostgreSQL table to a JSON file

Ever wanted to export data from the PostgreSQL table into a JSON file?

👇

pg_cmd="\\copy (select row_to_json(r) from (select * FROM table_name) r ) to
table.json"
psql -w -h host -U username -d database_name -c $pg_cmd
sed -i -e 's/\\\\/\\/g' table.json # clean up extra '\' characters

Enter fullscreen mode Exit fullscreen mode

After executing these commands you will have two JSON files

  1. table.json - initial JSON exported data file
  2. table.json-e - file which contains cleaned up JSON, without extra \ characters

✌️

💖 💪 🙅 🚩
dixpac
Dino

Posted on November 17, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related