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