Minimal postgres pivot table implementation Just copy the code in pgadmin and run it. It should work.