index.ts 719 B

1234567891011121314151617181920212223242526272829
  1. import { drizzle } from 'drizzle-orm/node-postgres';
  2. import { Pool } from 'pg';
  3. import * as dotenv from 'dotenv';
  4. dotenv.config();
  5. const pool = new Pool({
  6. connectionString: process.env.DATABASE_URL,
  7. });
  8. export const db = drizzle(pool);
  9. // 用于测试连接的函数
  10. export async function testConnection() {
  11. try {
  12. const client = await pool.connect();
  13. console.log('✅ 成功连接到 PostgreSQL 数据库!');
  14. const res = await client.query('SELECT NOW()');
  15. console.log('🕒 数据库当前时间:', res.rows[0].now);
  16. client.release();
  17. } catch (err) {
  18. console.error('❌ 数据库连接失败:', err);
  19. process.exit(1);
  20. }
  21. }
  22. if (require.main === module) {
  23. testConnection();
  24. }