As you see it is a really easy process, however, the driver implementation requires to have a database name postgres://username:password@host/database to be connected, which means you need to connect to a database first.
It's not because of the driver implementation, it's PostgreSQL itself. It's the same with any other language or driver.
A client needs to be connected to a database in order to do anything, including a CREATE DATABASE. Besides the postgres database, template1 is often used for this purpose too.
Then, since you must connect to the freshly created database to create objects inside it, there's no way to avoid opening another connection.
In short, what you're doing can't be simplified, it's already optimal.
Answer from Daniel Vérité on Stack OverflowAs you see it is a really easy process, however, the driver implementation requires to have a database name postgres://username:password@host/database to be connected, which means you need to connect to a database first.
It's not because of the driver implementation, it's PostgreSQL itself. It's the same with any other language or driver.
A client needs to be connected to a database in order to do anything, including a CREATE DATABASE. Besides the postgres database, template1 is often used for this purpose too.
Then, since you must connect to the freshly created database to create objects inside it, there's no way to avoid opening another connection.
In short, what you're doing can't be simplified, it's already optimal.
I've just written a module for that: https://github.com/olalonde/pgtools
var pgtools = require('pgtools');
pgtools.createdb({
user: 'postgres',
password: 'some pass',
port: 5432,
host: 'localhost'
}, 'test-db', function (err, res) {
if (err) {
console.error(err);
process.exit(-1);
}
console.log(res);
});
Hopefully it can make your code a bit cleaner.
node.js - How to create table after first creating a database with Node and pg - Stack Overflow
Connect react to postgresql database?
Connecting to Database via Vanilla JS and Node, Using Only Built in Modules.
How to Read Data from a PostgreSQL Database using JS, Node.JS, Express.JS, and pg-promise
For creating a database from code, I use the client instead of pool for this one. Here's the example:
const { Pool, Client } = require('pg')
const client = new Client({
user: 'postgres',
host: 'localhost',
password: 'postgres',
port: 5432
})
await client.connect()
await client.query(`DROP DATABASE IF EXISTS ${dbname};`)
await client.query(`CREATE DATABASE ${dbname};`)
await client.end()
//call the pool you just created after the database has been created.
Maybe the following code will help you. The table will now be created in the callback immediately after the "CREATE DATABASE" query has finished.
function createDatabase(){
const pool = new pg.Pool({
user: 'postgres',
host: '127.0.0.1',
database: 'postgres',
password: 'postgres',
port: '5432'}
);
pool.query("CREATE DATABASE myApp;", (err, res) => {
console.log(err, res);
pool.query("CREATE TABLE session(sessionguid UUID NOT NULL, created text NOT NULL, sessionlife integer NOT NULL)", (err, res) => {
console.log(err, res);
pool.end();
});
});
}