Postgres connection problem

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

Hi, @designpeak! Welcome to the Vercel Community :smile:

Thanks for your patience!

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?

  1. 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:

    POSTGRES_URL=postgres://username:password@hostname:port/database
    
  2. 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
    
  3. 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.

  4. 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;
    }
    
  5. Running the Development Server:
    After ensuring the environment variables are correctly set, you can run your development server using:

    npm run dev
    

    or

    vercel dev
    

Let us know how you got on!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.