VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString'

Hello everyone! So I'm getting the following error while trying to use the @vercel/postgres package with drizzle ORM
VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.
at createPool (webpack-internal:///(rsc)/./node_modules/@vercel/postgres/dist/chunk-VGUHM5WG.js:150:34)
at Object.get (webpack-internal:///(rsc)/./node_modules/@vercel/postgres/dist/chunk-VG
VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.
at createPool (webpack-internal:///(rsc)/./node_modules/@vercel/postgres/dist/chunk-VGUHM5WG.js:150:34)
at Object.get (webpack-internal:///(rsc)/./node_modules/@vercel/postgres/dist/chunk-VG
Here is my db index.ts
import { sql } from '@vercel/postgres';
import { drizzle } from 'drizzle-orm/vercel-postgres';

export const db = drizzle(sql)
import { sql } from '@vercel/postgres';
import { drizzle } from 'drizzle-orm/vercel-postgres';

export const db = drizzle(sql)
As well as my drizzle.config.ts
import { loadEnvConfig } from "@next/env";
import type { Config } from "drizzle-kit";
import { cwd } from "process";

loadEnvConfig(cwd());

console.log("url", process.env.DEV_POSTGRES_URL);

let connectionString;

if (process.env.VERCEL_ENV === "production") {
connectionString = process.env.PROD_POSTGRES_URL;
} else if (process.env.VERCEL_ENV === "preview") {
connectionString = process.env.STAGING_POSTGRES_URL;
} else if (process.env.VERCEL_ENV === "development") {
connectionString = process.env.DEV_POSTGRES_URL;
}

export default {
schema: "./src/lib/db/schema.ts",
out: "./drizzle",
driver: "pg",
dbCredentials: {
connectionString: connectionString
? connectionString + "?sslmode=require"
: "",
},
} as Config;
import { loadEnvConfig } from "@next/env";
import type { Config } from "drizzle-kit";
import { cwd } from "process";

loadEnvConfig(cwd());

console.log("url", process.env.DEV_POSTGRES_URL);

let connectionString;

if (process.env.VERCEL_ENV === "production") {
connectionString = process.env.PROD_POSTGRES_URL;
} else if (process.env.VERCEL_ENV === "preview") {
connectionString = process.env.STAGING_POSTGRES_URL;
} else if (process.env.VERCEL_ENV === "development") {
connectionString = process.env.DEV_POSTGRES_URL;
}

export default {
schema: "./src/lib/db/schema.ts",
out: "./drizzle",
driver: "pg",
dbCredentials: {
connectionString: connectionString
? connectionString + "?sslmode=require"
: "",
},
} as Config;
Few notes: 1. The URL is provided in connectionString. I'm sure it's defined. I have even hardcoded it. I'm 99% sure that the case is that Vercel postgres doesn't really read it and is trying to look for POSTGRES_URL in the env (which is not present because I want to use prefixes for each DB depending on the environment). 2. As you can probably already guess I'm trying to make it so depending on the environment I'm using a different DB. You got any advice on that?
7 Replies
Angelelz
Angelelz•15mo ago
This might be an issue on how drizzle-kit sets the connection string on the pg driver Have you tried this:
if (process.env.VERCEL_ENV === "production") {
connectionString = process.env.POSTGRES_URL = process.env.PROD_POSTGRES_URL;
} else if (process.env.VERCEL_ENV === "preview") {
connectionString = process.env.POSTGRES_URL = process.env.STAGING_POSTGRES_URL;
} else if (process.env.VERCEL_ENV === "development") {
connectionString = process.env.POSTGRES_URL = process.env.DEV_POSTGRES_URL;
}
if (process.env.VERCEL_ENV === "production") {
connectionString = process.env.POSTGRES_URL = process.env.PROD_POSTGRES_URL;
} else if (process.env.VERCEL_ENV === "preview") {
connectionString = process.env.POSTGRES_URL = process.env.STAGING_POSTGRES_URL;
} else if (process.env.VERCEL_ENV === "development") {
connectionString = process.env.POSTGRES_URL = process.env.DEV_POSTGRES_URL;
}
Mr Volcano
Mr VolcanoOP•15mo ago
Hey @Angelelz and thanks for the response, this actually works! I'm really surprised, do you mind sharing why this is working ? It looks like sorcery to me 😛
Angelelz
Angelelz•15mo ago
Yeah, you're just setting connectionString and process.env.POSTGRES_URL to the same value
Mr Volcano
Mr VolcanoOP•15mo ago
Hmm, didn't really get that. When I do connectionString = process.env.PROD_POSTGRES_URL I assign the env variable to connectionString. Why is connectionString = process.env.POSTGRES_URL = process.env.PROD_POSTGRES_URL any different ? Like, shouldnt drizzle be taking the variable from "connectionString" anyway ?
Angelelz
Angelelz•15mo ago
PROD_POSTGRES_URL gets assigned to POSTGRES_URL and that gets assigned to connectionString. Yes, I think it's an issue with drizzle-kit I'm just offering a workaround
Mr Volcano
Mr VolcanoOP•15mo ago
oohh okay good good. Now that makes a bit more sense.
Thank you very much, you're a wizard! Literally spent 5 hours straight trying to debug this
Angelelz
Angelelz•15mo ago
I mean, you did. I only gave the last push @Andrew Sherman Seems like the kit is not passing along the connectionString from the DB credential?
Want results from more Discord servers?
Add your server