Esquire Theme by Matthew Buchanan
Social icons by Tim van Damme

01

May

Copy rows from one table to another (SELECT to INSERT)

I always seem to need to copy a few rows from our production postgresql database which is remote to my local development database. I could use something like pgdump but I really just want to turn my SELECT statements into INSERT statements.

A stackoverflow post shows how to turn SELECT->INSERT for Postgresql but its still rather manual: http://stackoverflow.com/questions/1820650/sql-script-to-create-insert-script/10392124#10392124

So I have written a python script that will take a SELECT statement into INSERT statements. The script assumes that your selecting on all the columns in the table which is easy to do with pgAdmin III: right click on a table and select ‘Scripts->SELECT’.

Sometimes Tumblr messes up the Gist embed: https://gist.github.com/2568047

21

Nov

Replacement for unix ‘cut’ that works with CSV

A while ago I was working with a very large CSV file that had many columns but I only needed two columns of.

I normally would use the Unix command ‘cut’ to get the columns I want but this does not work well for CSV files that are escaped (that is the separator is escaped).

So here is a drop in replacement written in Python that works just like ‘cut’ but for almost all CSV files: