Skip to content

Instantly share code, notes, and snippets.

@clhenrick
Last active March 3, 2025 23:02
Show Gist options
  • Select an option

  • Save clhenrick/ebc8dc779fb6f5ee6a88 to your computer and use it in GitHub Desktop.

Select an option

Save clhenrick/ebc8dc779fb6f5ee6a88 to your computer and use it in GitHub Desktop.
PostgreSQL & PostGIS cheatsheet (a work in progress)

PostgreSQL & PostGIS Cheatsheet

The most common commands so I don't forget!

Postgres

  • to start the Postgres server do: postgres -D /usr/local/var/postgres

  • or do pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start to start and pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log stop to stop

  • to have Postgres start everytime you boot your Mac do: ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents then to check that it's working after booting do: ps ax | grep sql

psql

psql is the unix command line tool for interacting with postgres.

Common Commands

  • for doing admin type things log in as postgres: psql postgres;

  • to create a databse: CREATE database-name;

  • to delete a database DROP DATABASE database-name;

  • to connect to a database: \c database-name;

  • to connect when starting psql use the -d flag like: psql -d nyc_noise

  • to list all databases: \l

  • to quit psql: \q

  • to grant privileges to a user: GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;

  • to enable the hstore extension ( for key : value pairs) do: CREATE EXTENSION hstore

  • to view columns of a table: \d table_name

  • to rename a column: alter table noise.hoods rename column noise_sqkm to complaints_sqkm;

  • to change a column's data type: alter table noise.hoods alter column noise_area type float;

  • to compute values from two columns and assign them to another column: update noise.hoods set noise_area = noise/(area/1000);

  • create a new table for data from a CSV that has lat and lon columns:

    create table noise.locations
    (                                     
      name varchar(100),
      complaint varchar(100), descript varchar(100),
      boro varchar(50),
      lat float8,
      lon float8,
      geom geometry(POINT, 4326)
    );
    
  • importing data from a CSV file:

     COPY noise.locations (name, complaint, descript, boro, lat, lon)
     FROM '/Users/chrislhenrick/tutorials/postgresql/data/noise.csv' WITH CSV HEADER;
    
  • list all columns from a table in a db in alphabetical order:

    select column_name
    from information_schema.columns
    where table_schema = 'public'
    and table_name = 'bk_pluto'
    order by column_name;
    

PostGIS

PostGIS is the extension for Postgres that allows for working with geometry data types and doing GIS operations in Postgres.

Common Commands

  • to enable PostGIS on a regular Postgres table do: CREATE EXTENSION postgis;

  • to enable PostGIS topology do: CREATE EXTENSION postgis_topology;

  • to update a column from table A with the number of points from table B that intersect table A's polygons:

update noise.hoods set num_complaints = (
	select count(*)
	from noise.locations
	where
	ST_Intersects(
		noise.locations.geom,
		noise.hoods.geom
	)
);
  • calculating area in EPSG 4326:
    alter table noise.hoods set area = (select ST_Area(geom::geography));

  • inputing values for the geometry type after loading data from a CSV:
    update noise.locations set the_geom = ST_SetSRID(ST_MakePoint(lon, lat), 4326);

Spatial Indexing

Makes queries hella fast. OSGeo has a good tutorial.

  • Basically the steps are:
    CREATE INDEX table_name_gix ON table_name USING GIST (geom);
    VACUUM ANALYZE table_name
    CLUSTER table_name
    Do this after making changes to your dataset or importing new data.

Importing data to PostGIS

Using shp2pgsql

  1. Do:
    shp2pgsql -I -s 4326 nyc-pediacities-hoods-v3-edit.shp noise.hoods > noise.sql
    Or for using the geography data type do:
    shp2pgsql -G -I nyc-pediacities-hoods-v3-edit.shp noise.nyc-pediacities-hoods-v3-edit_geographic > nyc_pediacities-hoods-v3-edit.sql

  2. Do:
    psql -d nyc_noise -f noise.sql
    Or for the geography type above:
    psql -d nyc_noise -f nyc_pediacities-hoods-v3-edit.sql

Using osm2pgsql

To import an OpenStreetMap extract in PBF format do:
osm2pgsql -H localhost --hstore-all -d nyc_from_osm ~/Downloads/newyorkcity.osm.pbf

Using ogr2ogr

Example importing a GeoJSON file into a database called nyc_pluto:
ogr2ogr -f PostgreSQL PG:"host='localhost' user='chrislhenrick' port='5432' dbname='nyc_pluto' password=''" bk_map_pluto_4326.json -nln bk_pluto

Exporting data from PostGIS

Using ogr2ogr

Note: You may need to set the GDAL_DATA path if you git this error:

ERROR 4: Unable to open EPSG support file gcs.csv.
Try setting the GDAL_DATA environment variable to point to the
directory containing EPSG csv files.

If on Linux / Mac OS do this: export GDAL_DATA=/usr/local/share/gdal
If on Windows do this: C:\> set GDAL_DATA=C:\GDAL\data

To Export Data
Use ogr2ogr as follows to export a table (in this case a table called noise.hoods) to a GeoJSON file (in this case a file called nyc_hoods_noise.geojson):

ogr2ogr -f GeoJSON -t_srs EPSG:4326
nyc_hoods_noise.geojson PG:"dbname='name_of_database' user='user_name' password='your_password'" 
-sql "SELECT neighborho, borough, no_complaints, area_sqm, complaints_sqkm, geom FROM noise.hoods"
@curious000cat
Copy link

Hi! I keep needing to do this "If on Windows do this: C:> set GDAL_DATA=C:\GDAL\data" even though this GDAL_DATA is already set up in my windows variables. Any suggestions?

@xpostudio4
Copy link

Hi Henrick,

In create database you have CREATE database-name; but it should be CREATE DATABASE database-name;

I hope it helps

@clhenrick
Copy link
Author

thanks @xpostudio4 I will change it.
@curious000cat I'm not too familiar with windows but it looks like the variable isn't staying set. Try taking a look at this stackexchange answer, hope it helps.

@taozhi8833998
Copy link

no further update???

@clhenrick
Copy link
Author

@taozhi8833998 I just updated it today!

@fazlerabbi37
Copy link

list all the table in a database: \dt
list extensions installed in a database: \dx

@gamesbook
Copy link

gamesbook commented Oct 11, 2017

May I suggest using a different word for the dummy database name? PostgreSQL does not like a - in any names; so something like CREATE DATABASE database_name would be better...

@clhenrick
Copy link
Author

@gamesbook good point, changed them to database_name

@beedawg1985
Copy link

Loving this - good work! In terms of other ways to interact with Postgres, I found PGWeb a really neat, stable cross-platform client.

@albttx
Copy link

albttx commented May 29, 2018

Awesome ! Thank you :)

@Aruna48
Copy link

Aruna48 commented Mar 11, 2019

May I please know how to display the shapefiles using pygis? I have no basics as I am a beginner

@clhenrick
Copy link
Author

Make sure to checkout this guide on PostgreSQL which has detailed information on topics relating to set up and admin:
https://knowledgepill.it/posts/postgresql-basics-guide/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment