Skip to content

Instantly share code, notes, and snippets.

View grant-humphries's full-sized avatar

Grant Humphries grant-humphries

  • TriMet
  • Portland, OR, USA
View GitHub Profile
@grant-humphries
grant-humphries / sqlalchemy_where_exists.md
Last active August 1, 2023 08:44
SQLAlchemy where exists example

This python:

lg = LocationGroup
lgl1 = LocationGroupLocation
lgl2 = aliased(LocationGroupLocation)

sub_q1 = (
    self.db_session.query(lg).
 filter(lg.location_group_type_id == 2,
@grant-humphries
grant-humphries / windows_postgres_upgrade.md
Last active March 28, 2017 17:40
Upgrade PostgreSQL & PostGIS on Windows

This is currently incomplete

Upgrade PostgreSQL & PostGIS on Windows

  1. From cmd running as administrator create a Windows user called postgres with the following commands:

    :: if postgres user already exists delete it

net user postgres /delete

diff --git a/install.bat b/install.bat
index 6a3c4bc..57636de 100755
--- a/install.bat
+++ b/install.bat
@@ -5,8 +5,8 @@ set SCRIPT_PATH=%~dp0
set SCRIPT_PATH=%SCRIPT_PATH:\=/%
set CUSTOM=false
set INSTALLER_PATH=
-set BABUN_ZIP=%SCRIPT_PATH%/dist/babun.zip
-set UNZIPPER=%SCRIPT_PATH%/dist/unzip.exe
@grant-humphries
grant-humphries / bg.py
Created March 29, 2016 22:15
block group sqlalchemy model
# coding: utf-8
from geoalchemy2 import Geometry
from sqlalchemy import Column, Float, ForeignKey, Text
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
pg_url = 'postgres://{user}:{pw}@{host}/{db}'
engine = create_engine(pg_url)
metadata = MetaData(bind=ops.engine)
metadata.reflect(schema='tbl_schema', only=['tbl_name'])
fk_column = metadata.tables['tbl_schema.tbl_name'].columns['tbl_column']
fk = ForeignKey(fk_column)
# list of columns that should have the foreign key
fk_list = [...]
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce_
_', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_connection_cls', '_echo', '_execute
_clauseelement', '_execute_compiled', '_execute_default', '_execution_options', '_has_events', '_optional_conn_ctx_manager', '_run_visitor', '_sh
ould_log_debug', '_should_log_info', '_trans_ctx', '_wrap_pool_connect', 'begin', 'connect', 'contextual_connect', 'create', 'dialect', 'dispatch
', 'dispose', 'driver', 'drop', 'echo', 'engine', 'execute', 'execution_options', 'has_table', 'logger', 'logging_name', 'name', 'pool', 'raw_con
nection', 'run_callable', 'scalar', 'table_names', 'transaction', 'update_execution_options', 'url']
[buildout]
parts = census-pg-requirements
eggs =
fiona
gdal
geoalchemy2
psycopg2
shapely
sqlacodegen
sqlalchemy
# Some good references are:
# http://russbrooks.com/2010/11/25/install-postgresql-9-on-os-x
# http://www.paolocorti.net/2008/01/30/installing-postgis-on-ubuntu/
# http://postgis.refractions.net/documentation/manual-1.5/ch02.html#id2630392
#1. Install PostgreSQL postgis and postgres
brew install postgis
initdb /usr/local/var/postgres
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start