I tried connecting my Postgres Database to a Next.JS project and it didn’t quite work out for me.
I saw countless questions with my exact problem and none of them helped me.
Im getting this error VercelPostgresError: VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.
First i made a next js project, uploaded it to github and connected vercel and installed verce
Set up the postgres database and run these commands yarn global add vercel@latest yarn add @vercel/postgres vercel link vercel env pull .env.development.local
And then created new file named data.ts in my app/lib folder
import { sql } from "@vercel/postgres";
export async function fetchCarData(carName: string) {
const carDataPromise = sql`SELECT * FROM carinfo WHERE name=${carName};
`;
const data = await Promise.all([carDataPromise]);
return data;
}
I know i do not have to use Promise.all when i only have one promise but i plan on adding more.
Also tried running with either npm run dev and vercel dev
It looks like the error you’re encountering is because of a missing connection string for your Postgres database. The error message VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found. indicates that the environment variable POSTGRES_URL is not set.
Could you try the following?
Set the POSTGRES_URL Environment Variable: Make sure that your .env.development.local file contains the POSTGRES_URL variable with the correct connection string to your Postgres database. It should look something like this:
Verify Environment Variables:
After setting the POSTGRES_URL in your .env.development.local file, make sure to pull the environment variables again using:
vercel env pull .env.development.local
Check Your Vercel Project Settings:
Make sure that the environment variable POSTGRES_URL is also set in your Vercel project settings. You can do this by navigating to your project on the Vercel dashboard, going to the “Settings” tab, and then to “Environment Variables”. Add the POSTGRES_URL variable if it is not already present.
Use the Correct Connection Method:
Based on the context, it seems you are using the @vercel/postgres package. Make sure you are using the correct method to create a connection. Here is an example of how to use the sql function with the connection string:
import { sql } from "@vercel/postgres";
export async function fetchCarData(carName: string) {
const carDataPromise = sql`SELECT * FROM carinfo WHERE name=${carName};`;
const data = await Promise.all([carDataPromise]);
return data;
}
Running the Development Server:
After ensuring the environment variables are correctly set, you can run your development server using: