next-learn
next-learn copied to clipboard
An error occurred while attempting to seed the database: VercelPostgresError ..... no 'POSTGRES_URL' env var was found.
I am facing issue while following the create dashboard application from the documentation (chapter - 6): 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.
Screenshot of the error:
Here is how my seed.js looks like:
const { db } = require('@vercel/postgres');
const {
invoices,
customers,
revenue,
users,
} = require('../app/lib/placeholder-data.js');
const bcrypt = require('bcrypt');
async function seedUsers(client) {
try {
await client.sqlCREATE EXTENSION IF NOT EXISTS "uuid-ossp";
// Create the "users" table if it doesn't exist
const createTable = await client.sqlCREATE TABLE IF NOT EXISTS users ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, name VARCHAR(255) NOT NULL, email TEXT NOT NULL UNIQUE, password TEXT NOT NULL );;
console.log(Created "users" table);
// Insert data into the "users" table
const insertedUsers = await Promise.all(
users.map(async (user) => {
const hashedPassword = await bcrypt.hash(user.password, 10);
return client.sqlINSERT INTO users (id, name, email, password) VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword}) ON CONFLICT (id) DO NOTHING;;
}),
);
console.log(Seeded ${insertedUsers.length} users);
return {
createTable,
users: insertedUsers,
};
} catch (error) {
console.error('Error seeding users:', error);
throw error;
}
}
async function seedInvoices(client) {
try {
await client.sqlCREATE EXTENSION IF NOT EXISTS "uuid-ossp";
// Create the "invoices" table if it doesn't exist
const createTable = await client.sqlCREATE TABLE IF NOT EXISTS invoices ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, customer_id UUID NOT NULL, amount INT NOT NULL, status VARCHAR(255) NOT NULL, date DATE NOT NULL );;
console.log(Created "invoices" table);
// Insert data into the "invoices" table
const insertedInvoices = await Promise.all(
invoices.map(
(invoice) => client.sqlINSERT INTO invoices (customer_id, amount, status, date) VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date}) ON CONFLICT (id) DO NOTHING;,
),
);
console.log(Seeded ${insertedInvoices.length} invoices);
return {
createTable,
invoices: insertedInvoices,
};
} catch (error) {
console.error('Error seeding invoices:', error);
throw error;
}
}
async function seedCustomers(client) {
try {
await client.sqlCREATE EXTENSION IF NOT EXISTS "uuid-ossp";
// Create the "customers" table if it doesn't exist
const createTable = await client.sqlCREATE TABLE IF NOT EXISTS customers ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, image_url VARCHAR(255) NOT NULL );;
console.log(Created "customers" table);
// Insert data into the "customers" table
const insertedCustomers = await Promise.all(
customers.map(
(customer) => client.sqlINSERT INTO customers (id, name, email, image_url) VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url}) ON CONFLICT (id) DO NOTHING;,
),
);
console.log(Seeded ${insertedCustomers.length} customers);
return {
createTable,
customers: insertedCustomers,
};
} catch (error) {
console.error('Error seeding customers:', error);
throw error;
}
}
async function seedRevenue(client) {
try {
// Create the "revenue" table if it doesn't exist
const createTable = await client.sqlCREATE TABLE IF NOT EXISTS revenue ( month VARCHAR(4) NOT NULL UNIQUE, revenue INT NOT NULL );;
console.log(Created "revenue" table);
// Insert data into the "revenue" table
const insertedRevenue = await Promise.all(
revenue.map(
(rev) => client.sqlINSERT INTO revenue (month, revenue) VALUES (${rev.month}, ${rev.revenue}) ON CONFLICT (month) DO NOTHING;,
),
);
console.log(Seeded ${insertedRevenue.length} revenue);
return {
createTable,
revenue: insertedRevenue,
};
} catch (error) {
console.error('Error seeding revenue:', error);
throw error;
}
}
async function main() {
const client = await db.connect();
await seedUsers(client);
await seedCustomers(client);
await seedInvoices(client);
await seedRevenue(client);
await client.end();
}
main().catch((err) => {
console.error(
'An error occurred while attempting to seed the database:',
err,
);
});
Here is how my .env file looks like:
POSTGRES_VERCEL_DB_URL=""
POSTGRES_VERCEL_DB_PRISMA_URL=""
POSTGRES_VERCEL_DB_URL_NO_SSL=""
POSTGRES_VERCEL_DB_URL_NON_POOLING=""
POSTGRES_VERCEL_DB_USER=""
POSTGRES_VERCEL_DB_HOST=""
POSTGRES_VERCEL_DB_PASSWORD=""
POSTGRES_VERCEL_DB_DATABASE=""
AUTH_SECRET=""
AUTH_URL=http://localhost:3000/api/auth
Node Version: 20.9.0 OS: MacOS Monterey: Version - 12.6
you env doesn't have any values or removed them?
get the db creds from vercel postgres
try running vercel dev instead of npm run dev
that did the magic for me https://github.com/orgs/vercel/discussions/2350#discussioncomment-5850746
you env doesn't have any values or removed them?
I removed them for here only
I didn't do seed rather i ran the ddl and dml manually in vercel postgres ui and in my local i used vercel run.
I didn't do seed rather i ran the ddl and dml manually in vercel postgres ui and in my local i used vercel run.
Exactly, I had to use Prisma for the ddl and dml, they should resolve this issue whatever it is because even after my data is seeded in the remote Db, fetching using the @vercel/postgres still gives me the same error as above.
i had the same problem, what i did was put the: "seed": "node -r dotenv/config ./scripts/seed.js", before the : "start": "next start" Then i created the tables
rename .env.example to .env
rename .env.example to .env
Nice I had it named .env.local