GTFS SQL Import Tool


A Small Python Tool for Importing GTFS Data into SQL

This is a small utility for quickly and easily importing GTFS data into a SQL (i.e. relational) database. It is released under the MIT license.

Using GTFS SQL Importer

The code below shows how to import some data from a bash prompt.


PostgreSQL

    cat gtfs_tables.sql \
    <(python import_gtfs_to_sql.py path/to/gtfs/data/directory) \
    gtfs_tables_makeindexes.sql \
    vacuumer.sql \
    | psql mydbname myusername

SQLite

    cat gtfs_tables.sqlite \
    <(python import_gtfs_to_sql.py path/to/gtfs/data/directory nocopy) \
    | sqlite3 ANewDatabase.db
SQLite compatibility contributed by Justin Jones. See README for details.

Other Non-PostgreSQL

If you don't have PostgreSQL, then the python line should be as follows:

      <(python import_gtfs_to_sql.py path/to/gtfs/data/directory nocopy)
  
This will use "INSERT" statements instead of "COPY" statements. The vacuumer.sql file is also postgres specific and should be omitted.


Most GTFS data has errors in it, so you will likely encounter an error when running the above steps. After fixing the error by manually correcting the GTFS files, you can simply repeat the command (which will likely break again, and so on).

The source download includes a corrected version of the demo feed from the GTFS website, in the src/sample_feed directory. You should play around with that first to get everything to work and to see how the data gets put into tables.

Links

GTFS (General Transit Feed Specification)
GTFS Data Exchange
List of Public GTFS feeds