Node.js - PostgreSQL Create table

0 points
Created by:

In this article, we would like to show you how to create a table in Node.js.

const { Client } = require('pg');

const client = new Client({
    host: '',
    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) {
        return false;
    } finally {
        await client.end();         // closes connection

const text = `CREATE TABLE "users" (
	"id" SERIAL,
	"name" VARCHAR(100) NOT NULL,
	"role" VARCHAR(15) NOT NULL,

execute(text).then(result => {
    if (result) {
        console.log('Table created');


Database state after SQL CREATE TABLE query - HeidiSQL
Database state after SQL CREATE TABLE query - HeidiSQL


To create a multi-line string, create a template literal using backticks ``.

Alternative titles

  1. Node.js - PostgreSQL Create table with async/await
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - PostgreSQL

Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂