admin管理员组

文章数量:1122846

I am trying to set up a PostgreSQL database connection with TypeORM in my Next.js project using a DATABASE_URL string, but I'm facing issues getting the connection to work.

My current TypeORM setup (data-source.ts):

import { User } from "@/entities/User";
import "reflect-metadata";
import { DataSource } from "typeorm";

export const AppDataSource = new DataSource({
  type: "postgres",
  host: process.env.DB_HOST,
  port: parseInt(process.env.DB_PORT || "5432", 10),
  username: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  entities: [User],
  synchronize: true, // Set to false in production
  logging: true,
});

I would like to refactor my data-source.ts to directly use the DATABASE_URL string, so I don’t need to manage separate environment variables for host, port, username, etc. I want TypeORM to automatically parse the DATABASE_URL connection string for me.

My error:

Starting DB inilize!
 ⨯ error: no pg_hba.conf entry for host "119.xx.xxx.xx", user "postgres", database "postgres", no encryption
    at Socket.emit (node:events:514:28)

本文标签: How to connect TypeORM to PostgreSQL using a DATABASEURL connection string in NextjsStack Overflow