insert-data.js 463 B

12345678910111213141516171819202122232425
  1. const { Client } = require('pg')
  2. const client = new Client({
  3. user: 'postgres',
  4. host: 'localhost',
  5. database: 'testdb',
  6. password: '1234abcd',
  7. port: 5432,
  8. });
  9. client.connect()
  10. const query = `
  11. INSERT INTO users (email, firstName, lastName, age)
  12. VALUES ('johndoe@gmail.com', 'john', 'doe', 21)
  13. `
  14. client.query(query, (err, res) => {
  15. if (err) {
  16. console.error(err);
  17. return;
  18. }
  19. console.log('Data insert successful');
  20. });