| 12345678910111213141516171819202122232425262728293031 |
- import { drizzle } from 'drizzle-orm/node-postgres';
- import { Pool } from 'pg';
- import * as dotenv from 'dotenv';
- import * as authSchema from './schema/auth';
- import * as resourceSchema from './schema/resource';
- dotenv.config();
- const pool = new Pool({
- connectionString: process.env.DATABASE_URL,
- });
- export const db = drizzle(pool, { schema: { ...authSchema, ...resourceSchema } });
- // 用于测试连接的函数
- export async function testConnection() {
- try {
- const client = await pool.connect();
- console.log('✅ 成功连接到 PostgreSQL 数据库!');
- const res = await client.query('SELECT NOW()');
- console.log('🕒 数据库当前时间:', res.rows[0].now);
- client.release();
- } catch (err) {
- console.error('❌ 数据库连接失败:', err);
- process.exit(1);
- }
- }
- if (require.main === module) {
- testConnection();
- }
|