There appears to be a lot of documentation (e.g. https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js, but also elsewhere including this site) indic
If you want to stick with the code you have you can use an older version of Postgres.
First, apply:
npm uninstall postgresql
and then install version 6.1.2 (which is compatible with the code you mentioned):
npm install pg@6.1.2