I have a private RDS Postgres database and need to connect to it through my node application, but since it is private it is only accessible through my EC2 instance.
Before when my Postgres database was running locally I had something like this where the host was my localhost:
import pg from 'pg';
import { Kysely, PostgresDialect } from "kysely";
const pgPool = new pg.Pool({
database: process.env.DATABASE_NAME,
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
port: process.env.DATABASE_PORT,
});
const dialect = new PostgresDialect({
pool: pgPool,
});
export const db = new Kysely<DB>({
dialect,
});
To connect to the database through pgAdmin I had to use an SSH tunnel to first connect to the EC2 instance, and then connect to the database from that EC2 instance. I’m assuming it will require a similar approach to connect my node project to the database, but even after browsing fourms I still have no idea how this is typically done.