Skip to content

Instantly share code, notes, and snippets.

@glw
Last active July 12, 2022 19:56
Show Gist options
  • Save glw/ed56e222247d9eeb44e3a8954c2d1651 to your computer and use it in GitHub Desktop.
Save glw/ed56e222247d9eeb44e3a8954c2d1651 to your computer and use it in GitHub Desktop.
psycopg2 quick n easy connect to db
import geopandas as gpd
import psycopg2
# import psycopg2.extras
# If you want to use yaml create a config file
import yaml
# yaml config file load
config = yaml.safe_load(open("config.yaml"))
# postgresql db
HOST = config['postgresql']['HOST']
PORT = config['postgresql']['PORT']
USER = config['postgresql']['USER']
PASSWD = config['postgresql']['PASSWD']
DATABASE = config['postgresql']['DATABASE']
SCHEMA = config['postgresql']['SCHEMA']
con = psycopg2.connect(database=DATABASE, user=USER, password=PASSWD, port=PORT, host=HOST)
cur = con.cursor()
# Then you can do something like
table = "select * from public.table"
gdf = gpd.GeoDataFrame.from_postgis(table, con, geom_col='geom' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment