import-data.js 653 B

12345678910111213141516171819202122232425262728
  1. const pgp = require('pg-promise')();
  2. const fs = require('fs');
  3. // 连接到数据库
  4. const db = pgp({
  5. user: 'postgres',
  6. password: 'postgres',
  7. host: 'localhost',
  8. port: 25432,
  9. database: 'postgres'
  10. });
  11. let initDataSQL = fs.readFileSync("../data/init-data.sql").toString()
  12. // 执行SELECT语句并返回结果的函数
  13. async function main() {
  14. try {
  15. // 查询:数据库版本信息
  16. const data = await db.any(initDataSQL);
  17. console.log(data)
  18. return data;
  19. } catch (error) {
  20. console.error('Error executing query:', error);
  21. return null;
  22. }
  23. }
  24. // 调用函数并处理结果
  25. main()