Report this

What is the reason for this report?

Can't connect to PostgreSQL from DO function

Posted on February 20, 2023

Hello everyone,

I am trying to create a simple function that should return data from my database. However, I got :

2023-02-20T13:21:13.802551358Z stdout: SyntaxError: Unexpected reserved word

My code is:

function main(args) {
    const { Client } = require('pg');

    // create a new PostgreSQL client instance
    const client = new Client({
      user: 'ABC',
      host: 'ABC',
      database: 'defaultdb',
      password: 'ABC',
      port: 25060, // or the port that your Postgres instance is using
    });

    try {
      // connect to the database
      await client.connect();

      // perform some database operation, e.g. SELECT * FROM mytable
      const res = await client.query('SELECT * FROM order');

      // return the results as the response of the Lambda function
      return {
        statusCode: 200,
        body: JSON.stringify(res.rows),
      };
    } catch (err) {
      // handle any errors that occur during database connection or operations
      return {
        statusCode: 500,
        body: JSON.stringify({ message: err.message }),
      };
    } finally {
      // always close the database connection when done
      await client.end();
    }
} 

I would be grateful if someone helps me to sort this out

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.