Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const { Pool } = require('pg');
  4.  
  5. module.exports = {
  6. verifyDatabase: verifyDatabase
  7. };
  8.  
  9. async function verifyDatabase (db) {
  10. const pool = new Pool({
  11. user: 'postgres',
  12. host: '127.0.0.1',
  13. database: 'postgres',
  14. password: 'postgres',
  15. port: '5432'
  16. });
  17.  
  18. const { rows } = await pool.query(`SELECT count(*) > 0 as exist FROM pg_catalog.pg_database WHERE datname = '${db}'`);
  19.  
  20. if (!rows[0].exist) {
  21. await pool.query(`CREATE DATABASE "${db}"`);
  22. pool.end();
  23. } else {
  24. pool.end();
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement