I'm writing a Node.js web server that uses a Postgres database. I used to connect on each new request like this:
app.get('/', function (req, res) {
pg.connect(pgconnstring, function (err, client) {
// ...
});
});
But after a few requests, I noticed 'out of memory' errors on Heroku when trying to connect. My database has only 10 rows, so I don't see how this could be happening. All of my database access is of this form:
client.query('SELECT * FROM table', function (err, result) {
if (err) {
res.send(500, 'database error');
return;
}
res.set('Content-Type', 'application/json');
res.send(JSON.stringify({ data: result.rows.map(makeJSON) }));
});
Assuming that the memory error was due to having several persistent connections to the database, I switched to a style I saw in several node-postgres
examples of connecting only once at the top of the file:
var client = new pg.Client(pgconnstring);
client.connect();
app.get('/', function (req, res) {
// ...
});
But now my requests hang (indefinitely?) when I try to execute a query after the connection is disrupted. (I simulated it by killing a Postgres server and bringing it back up.)
So how do I do one of these?
PostgreSQL is a really popular, free, open-source relational database. The node-postgres module is a widely-employed and matured module that bridges Node.js to PostgreSQL. In this article, we've set up a PostgreSQL database and developed basic CRUD functionality through a simple Node.js script.
Another thing that comes with the installation is the psql – PostgreSQL interactive terminal. Running psql will connect you to a PostgreSQL host from the terminal and allow you to perform database operations from the command line. When you install Postgres, you get one default database named – postgres. So you’ll first connect to it via psql:
When you install Postgres, you get one default database named – postgres. So you’ll first connect to it via psql: You are now inside psql in the postgres database. You should see the text below in your terminal now, which means you’re connected to the postgres database as superuser, or root (the # mark is for superuser).
PostgreSQL will create a user called postgres to access the database in Linux-based platforms. Thus, we can use the following command to login as the postgres user: Then enter the CLI by running: You should see a command shell similar to this: To view the currently present databases, let's use the \list or \l command:
I'm assuming you're using the latest version of node-postgres, in which the connection pooling has been greatly improved. You must now check the connection back into the pool, or you'll bleed the connections:
app.get('/', function (req, res) {
pg.connect(pgconnstring, function (err, client, done) {
// do some stuff
done();
});
});
As for error handling on a global connection (#2, but I'd use the pool):
client.on('error', function(e){
client.connect(); // would check the error, etc in a production app
});
The "missing" docs for all this is on the GitHub wiki.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With