sql generation for postgres