I cannot connect to Render Postgres

I am trying to connect to my render postgres with this code:

import { Client } from 'pg';
async function createUsersTable() {
    // Set the environment variables for your database connection.
    const { PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DATABASE } = process.env;
    // Create a new client object.
    const client = new Client({
      url: "<my url (i wont show u)>"
    // Connect to the database.
    try {
      await client.connect();
    } catch (err) {
      if (err.code === 'ECONNREFUSED') {
        console.log('The database is not running.');
      } else {
        throw err;
    // Check if the users table already exists.
    const sql = `
        SELECT 1
        FROM information_schema.tables
        WHERE table_name = 'users'
    const result = await client.query(sql);
    // If the table does not exist, create it.
    if (result.rows[0].exists === false) {
      const createTableSql = `
        CREATE TABLE users (
          id SERIAL PRIMARY KEY,
          username VARCHAR(255) NOT NULL,
          password VARCHAR(255) NOT NULL
      await client.query(createTableSql);
    // Close the connection.
    await client.end();
    // Print a success message.
    console.log('The `users` table has been created.');
  // Call the createUsersTable function.

But I am getting the error ECONNREFUSED. It is running I don’t know what is going on. My environment variables look right…

Hi Tyler,

We’ll address this in the ticket you have open since it would be helpful to see the values you’re using for the environment variables.



1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.