supa-test/src/pg.ts
2024-10-20 19:45:15 +08:00

43 lines
1.0 KiB
TypeScript

import pg from 'pg';
// 使用pg链接数据库
// postgres: {
// username: 'root',
// host: 'light.xiongxiao.me',
// database: 'xpostgres',
// password: 'abearxiong',
// port: 5432,
// },
const pool = new pg.Pool({
user: 'postgres',
host: '124.222.140.199',
database: 'postgres',
password: '123456xx',
port: 5434,
});
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res);
pool.end();
});
// 配置数据库连接信息
// const client = new pg.Client({
// connectionString: 'postgresql://postgres:123456xx@light.xiongxiao.me:5434/postgres',
// });
// // 连接到数据库
// client.connect()
// .then(() => {
// console.log('Connected to the PostgreSQL database successfully!');
// return client.query('SELECT NOW()'); // 示例查询
// })
// .then((res) => {
// console.log('Server time:', res.rows[0]);
// })
// .catch((err) => {
// console.error('Database connection error:', err.stack);
// })
// .finally(() => {
// client.end(); // 关闭数据库连接
// });