As of now, the most popular and promising JavaScript client library for querying PostgreSQL database is node-postgres (pg on NPM). In order to use it with ES7 async/await constructs, it needs to be wrapped in ES6 Promises. There are a few existing solutions for that — pg-promise, pg-then, as well as many others. But first, let’s see if you really need one of those..

Here is an example, how the data fetching code looks like that uses node-postgres library as it is with its callback syntax inside an Express.js app:

While callbacks themselves don’t like that bad, you still need to pay attention to proper error handling, and either closing the existing connection or returning it back to the connection pool. You can create a custom tiny wrapper for node-postgres that will handle this logic for you, so that the code above could be rewritten as follows:

If you’re wondering how this core/db.js wrapper looks like, here it is:

That’s it, happy coding! :)

BTW, if you’re starting a new web app from scratch, check out this membership database schema boilerplate for PostgreSQL:

https://github.com/membership/membership.db/tree/master/postgres