getting missing connection string error even though everything is set up
Answered
ayrreervae posted this in #help-forum
just started learning nextjs and i am trying to setup my first postgres database. i have followed everything in the quick start guide and got the .env.development.local file generated with the vercel cli. but when i try to seed my db using seed.js
it gives out an error
const client=await db.connect();it gives out an error
An error occurred while attempting to seed the database:
VercelPostgresError: VercelPostgresError - 'missing_connection_string': You did not supply a 'connectionString' and no 'POSTGRES_URL' env var was found.
at createPool (D:\JITS\NextJS\jits-menu-manager\node_modules\@vercel\postgres\dist\chunk-5AO5NGV7.cjs:161:11)
at Object.get (D:\JITS\NextJS\jits-menu-manager\node_modules\@vercel\postgres\dist\chunk-5AO5NGV7.cjs:205:16)
at main (D:\JITS\NextJS\jits-menu-manager\scripts\seed.js:163:27)
at Object.<anonymous> (D:\JITS\NextJS\jits-menu-manager\scripts\seed.js:173:1)
...
at node:internal/main/run_main_module:28:49 {
code: 'missing_connection_string'
}Answered by Sun bear
After creating the file open
localhost:3000/api/test
And remove the
I would also rename main to createTableTest or something like this but anyways its just testing so doesnt matter
localhost:3000/api/test
And remove the
main() at the end of seed.js.I would also rename main to createTableTest or something like this but anyways its just testing so doesnt matter
23 Replies
my seed.js code
const { db } = require('@vercel/postgres');
async function main(){
const client = await db.connect();
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// Create the "users" table if it doesn't exist
const createTable = await client.sql`
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT NOT NULL
);
`;
await client.end();
}
main();Sun bear
Are the environment variables working?
It looks like there is no environment variable for "POSTGRES_URL"
When you run
It looks like there is no environment variable for "POSTGRES_URL"
When you run
npm run dev is it reading your .env.development.local file?it does
@ayrreervae it does
Sun bear
Okay and it contains the variable postgres_url?
I think I would add a console log in the beginning of your main()
Just to see if it is existing for testing
I think I would add a console log in the beginning of your main()
console.log(process.env.POSTGRES_URL)Just to see if it is existing for testing
undefinedweird
i got the scripts folder in the root directory and only got seed.js in it
node -r dotenv/config ./scripts/seed.jsSun bear
If you are using app router maybe adding
"use server"
at the top will solve the problem
"use server"
at the top will solve the problem
But in general this seed.js looks a little untypical.
You are calling the function in the end of the file. In general you wont do it like this. You would call the function in a page.tsx or route.ts.
You are calling the function in the end of the file. In general you wont do it like this. You would call the function in a page.tsx or route.ts.
I would recommend to create a file like
/api/test/route.ts
and then do something like
/api/test/route.ts
and then do something like
export async function GET(request: Request) {
await main()
return NextResponse.json({ test: "success?" }, { status: 200 });
}@Sun bear If you are using app router maybe adding
"use server"
at the top will solve the problem
i already tried that
i am just trying to seed the database
doing the same as i did while learning next-dashboard project
i am just trying to seed the database
doing the same as i did while learning next-dashboard project
worked then
doesn't work now
@ayrreervae okay
Sun bear
After creating the file open
localhost:3000/api/test
And remove the
I would also rename main to createTableTest or something like this but anyways its just testing so doesnt matter
localhost:3000/api/test
And remove the
main() at the end of seed.js.I would also rename main to createTableTest or something like this but anyways its just testing so doesnt matter
Answer
welp
that worked
but my problem is with seeding. i need it
that worked
but my problem is with seeding. i need it
@Sun bear After creating the file open
localhost:3000/api/test
And remove the `main()` at the end of seed.js.
I would also rename main to createTableTest or something like this but anyways its just testing so doesnt matter
it returned success and the table showed up in my vercel page
@ayrreervae it returned success and the table showed up in my vercel page
Sun bear
Good to hear. Yes it returned success because we hardcoded it but good that it shows up.
I would recommend to take a look at prisma. There you have good ways to handle migrations and also a great ORM to easier query the database
I would recommend to take a look at prisma. There you have good ways to handle migrations and also a great ORM to easier query the database
do it be free?
@ayrreervae do it be free?
Sun bear
Yes prisma is free. You can also use other ORMs
https://vercel.com/docs/storage/vercel-postgres#orm-compatibility
https://vercel.com/docs/storage/vercel-postgres#orm-compatibility
I prefer bandwagonning and will just go with that's popular
Prisma looks incredible easy