How to create tables?

Fforehead48864/24/2023
So my understanding is that running a "migration" means creating the table with the schema in your db.
My understanding is the code below should do it as per the docs. I've also separately run npx drizzle-kit generate:pg beforehand to generate my schema.
// db.ts
const pool = new Pool({
connectionString: process.env.POSTGRES_DB_URL,
});

const db = drizzle(pool, { logger: true });

(async () => {
await migrate(db, { migrationsFolder: "./drizzle" });
})();
// db.ts
const pool = new Pool({
connectionString: process.env.POSTGRES_DB_URL,
});

const db = drizzle(pool, { logger: true });

(async () => {
await migrate(db, { migrationsFolder: "./drizzle" });
})();
1) Is there a better way to run migrate via CLI before the app starts? Currently migrate runs whenever a route is hit... I tried running the drizzle-kit introspect:pg --out=migrations/ --connectionString=postgresql://user:pass@host:port/db_name command with my own details but it doesn't create the tables - I assume because introspect doesn't create and only checks your db to see if it conforms? 2) So my understanding is that the default behaviour is to run the migration whenever the first route is hit when your app is running? So when I query my route for the first time and my tables are not created, I get the error, error - unhandledRejection: error: no schema has been selected to create in when it tries to create a table. How can I fix this? Or alternatively, what is the recommended way to create our tables? Should we be pasting the sql into psql or smth?

Looking for more? Join the community!