Unable to connect to supabase database

87 Views Asked by At

I am trying to connect to the supabase database using both pgadmin and using the pg library but as soon as the db connects I receive the error Error: getaddrinfo ENOTFOUND aws-0-ap-south-1.pooler.supabase.com

Here is my connection code

//pgconfig.cjs
const { Pool } = require("pg");
const fs = require("fs");
require("dotenv").config();

const pool = new Pool({
  host: "aws-0-ap-south-1.pooler.supabase.com",
  port: 5432,
  database: "postgres",
  user: "postgres.ilquqvtggidzmibcpoze",
  password: "******", //no special chars or numbers
});
try {
  pool.connect(); 
  console.log("Connected to the database.");
} catch (error) {
  console.error("Error connecting to the database:", error.message);
}
module.exports = pool;

here are my db routes which give 404 and enotfound error

//dbroutes.js
import express from "express";
import pool from "../config/pgConfig.cjs";
const router = express.Router();
router.post("/newUser", async (req, res, next) => {
  try {
    const { email, password } = req.body; // destructure directly
    console.log("email:", email, "password:", password);

    const query =
      "INSERT INTO testes (email, password) VALUES ($1, $2) RETURNING *";
    const values = [email, password];
    const result = await pool.query(query, values);
    console.log("Success in newUser");
    res.json(result.rows); // send a response back to the frontend
  } catch (err) {
    console.log("error in regNewUser", err);
    next(err);
  }
});
router.get("/checkDatabase", async (req, res, next) => {
  try {
    const query = "SELECT 'Database is working' AS status";
    const result = await pool.query(query);

    res.json(result.rows);
  } catch (err) {
    console.log("Error checking database:", err);
    next(err);
  }
});

and here is the error log error log

0

There are 0 best solutions below