drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Postgres connection don't work with URL property

Open Zinnavoy opened this issue 10 months ago • 1 comments

Report hasn't been filed before.

  • [x] I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

1.0.0-beta.10-4a43a22

What version of drizzle-kit are you using?

1.0.0-beta.10-4a43a22

Other packages

No response

Describe the Bug

Using Bun with Bun's SQL as client.

Drizzle can't setup a socket connection with a postgres database when using the URL property in drizzle's configuration:

// drizzle.config.ts
import { defineConfig } from 'drizzle-kit';

import { Environment } from '@/environment';

export default defineConfig({
  verbose: Environment.NODE_ENV !== 'production',

  out: './src/migrations',
  schema: './src/entities/index.ts',

  dialect: 'postgresql',
  casing: 'snake_case',
  dbCredentials: { 
    url: Environment.DATABASE_URL,
  },
});
$ bun -b run drizzle-kit migrate
No config path provided, using default 'drizzle.config.ts'
Reading config file '/home/theseus/hobby/anchorhold/packages/database/drizzle.config.ts'
Using 'bun' driver for database querying
[⣷] applying migrations... Error  Failed query: CREATE SCHEMA IF NOT EXISTS "drizzle"
params:
error: "drizzle-kit" exited with code 1
error: script "migrate" exited with code 1

Although the error is silent/missing when using drizzle-kit, we can see a better error log when trying to run any query with a drizzle-orm instance:

307 |     } = options, password = options.password;
308 |     try {
309 |       if (typeof password === "function")
310 |         password = password();
311 |       if (password && @isPromise(password))
312 |       return createPostgresConnection(hostname, Number(port), username || "", password || ""
, database || "", sslMode || SSLMode.disable, tls || null, query || "", path || "", onConnected, o
nClose, idleTimeout, connectionTimeout, maxLifetime, !prepare);
                                           ^
error: FailedToOpenSocket failed to connect to postgresql
      at createConnection (internal:sql/postgres:312:38)
      at createConnection (internal:sql/postgres:292:35)
      at #startConnection (internal:sql/postgres:365:70)
      at #startConnection (internal:sql/postgres:364:27)
      at new PooledPostgresConnection (internal:sql/postgres:362:118)
      at connect (internal:sql/postgres:720:65)
      at queryFromPoolHandler (bun:sql:46:17)
      at #runAsync (internal:sql/query:80:21)

This works:

// drizzle.config.ts
import { defineConfig } from 'drizzle-kit';

import { Environment } from '@/environment';

export default defineConfig({
  verbose: Environment.NODE_ENV !== 'production',

  out: './src/migrations',
  schema: './src/entities/index.ts',

  dialect: 'postgresql',
  casing: 'snake_case',
  dbCredentials: {
    host: Environment.DATABASE_HOST,
    port: Environment.DATABASE_PORT,
    user: Environment.DATABASE_USER,
    password: Environment.DATABASE_PASSWORD,
    database: Environment.DATABASE_NAME,
  },
});
$ bun -b run drizzle-kit migrate
No config path provided, using default 'drizzle.config.ts'
Reading config file '/home/theseus/hobby/anchorhold/packages/database/drizzle.config.ts'
Using 'bun' driver for database querying
[✓] migrations applied successfully!%

Zinnavoy avatar Jan 14 '26 15:01 Zinnavoy

Hello @Zinnavoy

I was able to reproduce this bug by providing an invalid URL. Could you please confirm that Environment.DATABASE_URL is imported correctly and that its value is valid?

I will close this issue for now. Feel free to reopen it at any time and tag me

AleksandrSherman avatar Jan 19 '26 11:01 AleksandrSherman