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.
Answer from wacsd on Stack Overflow
🌐
YouTube
youtube.com › watch
Node.JS How to CREATE TABLE query in PostgreSQL Database - YouTube
Learn How to CREATE TABLE query on PostgreSQL Database in Node.js.
Published   February 28, 2019
Views   6K
Discussions

How can I create a table using Node.js?
I would create a Postgres function that creates the table. You can then call the function as a Supabase RPC. I haven't done this, but unless Supabase went to extraordinary lengths to prevent this sort of thing, it should work. https://dba.stackexchange.com/questions/42924/postgresql-function-to-create-table https://supabase.com/docs/reference/javascript/rpc More on reddit.com
🌐 r/Supabase
3
0
August 17, 2023
How to create a table with postgresql in nodejs without an ORM?
please I am trying to create a table with PostgreSQL using nodejs but I don't know how to go about it. I want the table to be created from the query without the use of an ORM More on stackoverflow.com
🌐 stackoverflow.com
node.js - node-postgres create database - Stack Overflow
I am using node-postgres, and at the beginning of my application I want to check whether the database exists or not. So my workflow idea is as following: Check whether myDb is existing If it is the... More on stackoverflow.com
🌐 stackoverflow.com
Create multiple db tables with nodejs for postgresql
I'm working on a project with nodejs and postgresqlDB. What I want to do is, to setup a schema with some tables, which have also foreign keys to each other in it. The problem on my actual solution ... More on stackoverflow.com
🌐 stackoverflow.com
May 4, 2019
🌐
Dirask
dirask.com › posts › Node-js-PostgreSQL-CREATE-TABLE-AS-10rMLp
Node.js - PostgreSQL CREATE TABLE AS
Node.js level. ... Note: at the end of this article you can find database preparation SQL queries. const { Client } = require('pg'); const client = new Client({ host: '127.0.0.1', user: 'my_username', database: 'my_database', password: 'my_password', port: 5432, }); const newTableFromQuery = async () => { const query = ` CREATE TABLE "users_without_email" AS SELECT * FROM "users" WHERE "email" IS NULL; `; await client.connect(); // creates connection try { await client.query(query); // sends query } finally { await client.end(); // closes connection } }; newTableFromQuery() .then(() => console.table('New table created!')) .catch(error => console.error(error.stack));
🌐
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
A config/config.json file that will contain the necessary configuration to connect to our database in development, staging, and production environments. A models/ directory which will have models.
🌐
Stack Overflow
stackoverflow.com › questions › 58645460 › how-to-create-a-table-with-postgresql-in-nodejs-without-an-orm
How to create a table with postgresql in nodejs without an ORM?
const pg = require('pg'); const ... }); db.query(“CREATE TABLE TABLE_NAME(id SERIAL PRIMARY KEY, xzy VARCHAR(40) NOT NULL, abc VARCHAR(40) NOT NULL)”, (err, res) => { console.log(err, res); db.end(); });...
🌐
w3resource
w3resource.com › PostgreSQL › snippets › postgresql-node-setup.php
PostgreSQL with Node.js: Setup, Configuration, and CRUD Examples
December 23, 2024 - Install pg: Installs the Node.js PostgreSQL library for managing database connections. Database Configuration: Configures database credentials, such as username, password, and host. Table Creation: Creates a table with columns for user ID, name, email, and timestamp.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › using-postgresql-with-nodejs-and-node-postgres
Using PostgreSQL with Node.js and node-postgres
March 6, 2020 - The configuration and access/database creation slightly differs between operating systems, though. In this article, we'll will be using Ubuntu 18.04, which is a popular Linux platform and includes PostgreSQL by default. Some steps might be a little bit different if you are using a different operating system. Let's get started with a simple blank Node.js project with the default settings:
🌐
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 - This is a simple guide on creating a set of CRUD APIs. As long as you know the basics of Node and Express, you can follow along. Some experience with SQL databases (especially PostgreSQL) will help you understand this guide much better. ... We have our express server. Let's create a table in PostgreSQL.
🌐
Packt
packtpub.com › en-us › learning › how-to-tutorials › how-setup-postgresql-nodejs
How to Setup PostgreSQL with Node.js
Now, this should give us the output CREATE TABLE,but if you want to list all tables in a database, you shoulduse the dt command. 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.
🌐
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.
🌐
Medium
medium.com › @mavericks-db › how-to-connect-postgresql-with-node-js-bd2cbe87aa9e
How to Connect PostgreSQL with Node.JS | by Mavericks Balitaan | Medium
February 15, 2023 - CREATE DATABASE your_database_name; CREATE TABLE IF NOT EXISTS users ( userid serial PRIMARY KEY, lastname varchar(255) UNIQUE NOT NULL, firstname text NOT NULL, location text NOT NULL ); INSERT INTO users (lastname, firstname, location) VALUES ('Doe', 'John', 'Remote'); ... Create a database.js file in the root directory....
🌐
Medium
medium.com › @fanbubu0 › efficient-database-setup-for-node-js-projects-using-postgresql-and-node-postgres-515302913ae9
Efficient Database Setup for Node.js Projects using PostgreSQL and Node-postgres | by William El France | Medium
January 16, 2024 - Begin by importing the required connection module, ensuring a seamless link between your Node.js application and the PostgreSQL database. ... Step 2: Defining Table Schemas Define table schemas using SQL queries for creating “Tags” and ...
🌐
Stack Overflow
stackoverflow.com › questions › 55980973 › create-multiple-db-tables-with-nodejs-for-postgresql
Create multiple db tables with nodejs for postgresql
May 4, 2019 - Try to swtich to then-catch schema or update your node version (sudo npm install -g n && sudo n stable).
🌐
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 ...
🌐
Dirask
dirask.com › posts › Node-js-PostgreSQL-Create-table-if-not-exists-DZXJNj
Node.js - PostgreSQL Create table if not exists
const { Client } = require('pg'); const client = new Client({ host: '127.0.0.1', user: 'postgres', database: 'database_name', password: 'password', port: 5432, }); const execute = async (query) => { try { await client.connect(); // gets connection await client.query(query); // sends queries return true; } catch (error) { console.error(error.stack); return false; } finally { await client.end(); // closes connection } }; const text = ` CREATE TABLE IF NOT EXISTS "users" ( "id" SERIAL, "name" VARCHAR(100) NOT NULL, "role" VARCHAR(15) NOT NULL, PRIMARY KEY ("id") );`; execute(text).then(result => { if (result) { console.log('Table created'); } });
🌐
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.
🌐
Untitled Publication
sodiqfarhan.hashnode.dev › building-a-nodejs-app-with-postgres-database-on-render-a-step-by-step-guide-beginner-friendly
Building a Node.js App with Postgres Database on Render: A Step-by-Step Guide (Beginner Friendly)
February 25, 2023 - Click on "New PostgreSQL" inside that section. On the next page, you'd be able to set up your database. Input name, database name and user. You can find out more about each question by hovering around the "i" icon by the side of its label. Don't worry about the "Datadog API key". Click on "Create Database". You'd be referred to another page and it may take a little while for the database to be completely set up. Create a table in the new DB after creating the database, the next thing will be to create a table in which data can be sent to and read from.