I struggled a lot to find a way to create (good) geojson files from a Postgis database. Today I found the answer and I hope this helps other people that are trying to do the same.
First, you have to create your Postgis spatial database. Here is an excellent article by Steve Gattuso on how to set this up: https://www.stevegattuso.me/2019/05/29/postgis-reverse-geocoder.html. Now that you have your database, lets head to the main part of this article.
I used Python’s Pandas to store the query’s info and psycopg2 for the database connection. Feel free to use another Postgres database adapter.