Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. const pg = require('pg');
  2.  
  3. const config = {
  4. host: 'mypgserver-20170401.postgres.database.azure.com',
  5. user: 'mylogin@mypgserver-20170401',
  6. password: '<server_admin_password>',
  7. database: '<name_of_database>',
  8. port: 5432,
  9. ssl: true
  10. };
  11.  
  12. const client = new pg.Client(config);
  13.  
  14. client.connect(err => {
  15. if (err) throw err;
  16. else { queryDatabase(); }
  17. });
  18.  
  19. const queryDatabase = () => {
  20.  
  21. const query = `
  22. DROP TABLE IF EXISTS inventory;
  23. CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);
  24. INSERT INTO inventory (name, quantity) VALUES ('banana', 150);
  25. INSERT INTO inventory (name, quantity) VALUES ('orange', 154);
  26. INSERT INTO inventory (name, quantity) VALUES ('apple', 100);
  27. `;
  28.  
  29. client.query(query, err => {
  30. console.log('Connection established');
  31.  
  32. if (err) throw err;
  33. else {
  34. client.end(err => {
  35. if (err) throw err;
  36. // Else closing connection finished without error
  37. console.log('Closed client connection');
  38. });
  39.  
  40. console.log('Finished execution, exiting now');
  41. process.exit();
  42. }
  43. });
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement