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 Overflow
🌐
Medium
medium.com › @aashisingh640 › node-js-postgresql-create-database-if-it-doesnt-exist-1a93f38629ab
Node.js + PostgreSQL — Create database if it doesn’t exist | by Aashi Singh | Medium
July 16, 2023 - If you check in your local, the database will be created! ... You can run this file manually by running the command ts-node setup.ts or you can add it as a script in package.json so as to always run before starting the project — “prestart”: “ts-node setup.ts”
Discussions

node.js - How to create table after first creating a database with Node and pg - Stack Overflow
I'm trying to create a node app that can set itself up on the database end by creating a database then the table and fields after. Below are the two functions I'm using to do each task independentl... More on stackoverflow.com
🌐 stackoverflow.com
Connect react to postgresql database?
Roughly speaking, here's the standard flow from any web app from the client to the database: JS code makes an AJAX call to a URL on the server A running web app server (such as one built with Express) has a handler function set up for that URL The URL handler extracts parameters from the request, and opens up a connection to the database It queries data from the DB, and then formats the data appropriately (such as turning the DB values into a JSON object) It then completes responding to the original request by sending back the data The client code takes the response data and does something with it, such as calling this.setState({items : response.data}) in a React component. So, yes, a typical web app server setup that uses Node.js might be built with Express to handle HTTP requests, and use a package like node-postgres to execute queries against the database. The Express app might have defined a REST API structure that it handles, or it might be using some other API definition structure instead. Note that none of that is React-specific, except for what your client-side code does with the data it's received from the server. Hopefully that gives you a slightly better idea how the pieces fit together. More on reddit.com
🌐 r/reactjs
6
9
August 7, 2018
Connecting to Database via Vanilla JS and Node, Using Only Built in Modules.​
Why do you want to do this? The GitHub repositories for the Postgres and Mongo drivers is probably your best to start. This doesn't feel like a subject that people write tutorials about. More on reddit.com
🌐 r/node
5
11
September 26, 2018
How to Read Data from a PostgreSQL Database using JS, Node.JS, Express.JS, and pg-promise
Saved for when I’m not at the bar. Thanks! More on reddit.com
🌐 r/node
5
49
November 21, 2019
🌐
Dirask
dirask.com › posts › Node-js-PostgreSQL-Create-database-pOv9vD
Node.js - PostgreSQL Create database
PostgreSQL database in · Node.js. const { Client } = require('pg'); const client = new Client({ host: '127.0.0.1', user: 'postgres', password: 'password', port: 5432, }); const createDatabase = async () => { try { await client.connect(); // ...
🌐
Tinloof
tinloof.com › blog › how-to-create-manage-a-postgres-database-in-node-js-from-scratch-tutorial
How to Create a Postgres database in NodeJS · Tutorial
An ORM avoids our backend from having long SQL queries all over the place and offers other features to reliably manage our database. In the next few steps, we will install and use Sequelize, a NodeJS ORM. Let's first create an empty project folder blog and initialize it with npm: ... sequelize-cli allows to quickly bootstrap a useful initial boilerplate that saves us some time. Let's do so by running the following command: ... 1Created "config/config.json"2Successfully created models folder at ".../blog/models".3Successfully created migrations folder at ".../blog/migrations".4Successfully created seeders folder at ".../blog/seeders".
🌐
Medium
medium.com › @eliaai › create-simple-psql-database-in-node-js-3434ca00953
Create Simple PSQL Database in Node.js | by Elia AI | Medium
February 9, 2018 - This post focuses on creating a PostgreSQL (PSQL) client for node.js database and show you how to do the the simple CRUD (create, read…
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-postgresql-with-node-js-on-ubuntu-20-04
How To Use PostgreSQL With Node.js on Ubuntu 20.04 | DigitalOcean
November 30, 2021 - Next, you’ll log in to the PostgreSQL interactive shell using the fish_user and create a table. In this section, you’ll open the Postgres shell with the user you created in the previous section on Ubuntu. Once you login into the shell, you’ll create a table for the Node.js app.
🌐
DEV Community
dev.to › napoleon039 › how-to-create-a-simple-postgresql-database-with-expressjs-58n8
How to create a simple PostgreSQL database with Expressjs? - DEV Community
July 10, 2023 - After extracting the name and email sent in the request, we use the query function to create a new record in the database. If you have used MySQL with Express before, this syntax will be very familiar. We can read the data for all users or a single one: These three functions also demonstrate how parameters are passed to SQL statements in node-postgres. If you have five parameters, then each parameter will be picked from the array in order. For example: ... If you have never worked with PostgreSQL before, I hope you try it.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › using-postgresql-with-nodejs-and-node-postgres
Using PostgreSQL with Node.js and node-postgres
March 6, 2020 - In this article, we've set up a PostgreSQL database and developed basic CRUD functionality through a simple Node.js script.
🌐
DEV Community
dev.to › fredabod › a-simple-nodejs-app-using-postgres-and-express-2179
A Simple Nodejs App using Postgres and Express - DEV Community
October 31, 2023 - You can do this after logging in to your psql with either cmd or PgAdmin app. On cmd you can do this psql -U postgres you'll be prompted to input your password. The command simply means I'm logging in to psql with the user postgres.
🌐
Packt
packtpub.com › en-us › learning › how-to-tutorials › how-setup-postgresql-nodejs
How to Setup PostgreSQL with Node.js
For the sake of this example, we are going to add a row to the table so that we have some data to play with and prove that this works. When you want to add something to a database in PostgreSQL, you use the INSERT command.
🌐
Abed's Blog
abedshamia.hashnode.dev › create-a-database-build-script-using-node-and-postgresql
Create a Database Build Script Using Node and PostgreSQL
March 10, 2022 - In the terminal run node app.js and check if the server is running. Now we made sure that server is ready, let's set up our PostgreSQL database configurations.
🌐
DEV Community
dev.to › glaucia86 › developing-a-crud-node-js-application-with-postgresql-4c9o
Developing a CRUD Node.js Application with PostgreSQL - DEV Community
February 21, 2021 - Article which teaches how to develop a CRUD application with Node.js & PostgreSQL focusing on the Back-End. Tagged with tutorial, node, postgres, webdev.
🌐
EnterpriseDB
enterprisedb.com › postgres-tutorials › how-quickly-build-api-using-nodejs-postgresql
How to quickly build an API using Node.js & PostgreSQL | EDB
This article describes how you can use Node.js and PostgreSQL to create an API and provides an example for how to create a table.
🌐
Northflank
northflank.com › guides › connecting-to-a-postgresql-database-using-node-js
Connecting to a PostgreSQL database using Node.js — Northflank
November 25, 2021 - In this how-to guide, we have shown how to use Node.js to connect to a PostgreSQL instance and how to manipulate and read data. In the first step, a client is instantiated. This client is then used to create a database table and rows are inserted ...
🌐
DEV Community
dev.to › aguowisdom › connecting-to-a-postgresql-database-with-nodejs-a-step-by-step-guide-n6j
CONNECTING TO A POSTGRESQL DATABASE WITH NODE.JS: A STEP-BY-STEP GUIDE - DEV Community
September 3, 2023 - Open your terminal and enter the following command: ... Replace your_database_name with a name for your database. This will create a new database with the specified name. Now that we have a database set up, let's create the necessary files to ...
🌐
The Odin Project
theodinproject.com › lessons › nodejs-using-postgresql
Using PostgreSQL | The Odin Project
It is a library that we’ll use to interface with the PostgreSQL db. Install it with: ... We can then initialize it in our application with the necessary connection information. Create a db folder, and a new file db/pool.js with:
🌐
Newline
newline.co › @kristiandupont › introduction-to-using-postgresql-with-nodejs-a-beginners-guide--557a41ec
Introduction to using PostgreSQL with NodeJS: A Beginner's Guide
July 17, 2024 - Are you ready to dive into the world of powerful database management with PostgreSQL and NodeJS? This guide is designed for beginners who want to understand how to use PostgreSQL in their NodeJS projects. Whether you're new to databases or looking to expand your skills, this tutorial will help ...
🌐
ScaleGrid
scalegrid.io › blog › how-to-connect-postgresql-database-in-node-js
How To Connect PostgreSQL Database In Node.Js | ScaleGrid
December 17, 2023 - If you don’t have PostgreSQL ... prerequisites in place, it’s time to set up your Node.js project. Start by creating a new directory for your project....
🌐
Medium
medium.com › dailyjs › node-js-postgresql-tutorial-7a19d945767f
Node.js Postgresql tutorial: Build a simple REST API with Express | by Geshan Manandhar | DailyJS | Medium
January 14, 2021 - You can, of course, carry on with this Node.js with PostgreSQL tutorial :). For your convinience, each step has been carved out as a distinct pull reqeust so that you can follow the tutorial with ease. You have Node.js (preferably Node 14.x) installed and running on your machine (or Node.js running with a docker container). You are aware of how Node.js generally works and also have a bit of Express Js experience. Having some knowledge of Git and GitHub will be really useful. For the database we will use a free database on ElephantSQL, so please register and set up a free PostgreSQL database there.