import psycopg2 import json # Connect to your PostgreSQL database conn = psycopg2.connect( host="localhost", database="nws", user="nws", password="nws" ) # Create a cursor object cur = conn.cursor() # Execute the SQL query cur.execute(""" SELECT json_build_object( 'type', 'FeatureCollection', 'features', json_agg( json_build_object( 'type', 'Feature', 'geometry', ST_AsGeoJSON(geom)::json, 'properties', json_build_object( 'county', countyname, 'state', state, 'lat', lat, 'lon', lon ) ) ) ) FROM county WHERE cwa = 'RLX'; """) # Fetch the result geojson_result = cur.fetchone()[0] # Print the GeoJSON result outfile = open("rlxtest.json", "w") json.dump(geojson_result,outfile, indent=2) # Close the cursor and connection cur.close() conn.close()