34 lines
857 B
JavaScript
34 lines
857 B
JavaScript
const { Pool } = require('pg');
|
|
require('dotenv').config();
|
|
|
|
const createTableQuery = `
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id SERIAL PRIMARY KEY,
|
|
email VARCHAR(255) UNIQUE NOT NULL,
|
|
name VARCHAR(255),
|
|
user_type VARCHAR(50) DEFAULT 'audience' NOT NULL
|
|
);
|
|
`;
|
|
|
|
async function createTable() {
|
|
const pool = new Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
});
|
|
|
|
try {
|
|
console.log('Connecting to the database...');
|
|
const client = await pool.connect();
|
|
|
|
console.log('Creating users table if it does not exist...');
|
|
await client.query(createTableQuery);
|
|
|
|
console.log('Users table created or already exists.');
|
|
client.release();
|
|
} catch (error) {
|
|
console.error('Error creating users table:', error);
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
createTable(); |