Connection to postgres from typescript hangs


TLDS! (TOO LONG DO SKIM)! The answer is long and rich! You can skim ! It’s well formated!

If you are in big hurry! You can check Authenticate section, Sequelize-typescript (not sequelize) section, Sequelize-typescript section.

And better you can go directly to HELL section! Get to know nodejs v14 HELL! (Go directly to the end! Well a bit above).

Check too FIX (Postgres v14 HELL)

I started and before knowing i found myself wrote too much!


Basically sequelize should not just hang! But throw an error!

Looking at code source

By looking at sync code here

 async sync(options) {
    // ...

    // no models defined, just authenticate
    if (!models.length) {
      await this.authenticate(options);
    } else {
      for (const model of models) await model.sync(options);
    if (options.hooks) {
      await this.runHooks('afterBulkSync', options);
    return this;

One easily can see the hanging possiblities!


To debug such anomalies first of all it’s important to have good logging!

And you can check how to add logging here! Even though normally sequelize have the logging of the query activated by default!

const sequelize = new Sequelize('sqlite::memory:', {
  // Choose one of the logging options
  logging: console.log,                  // Default, displays the first parameter of the log function call
  logging: (...msg) => console.log(msg), // Displays all log function call parameters
  logging: false,                        // Disables logging
  logging: msg => logger.debug(msg),     // Use custom logger (e.g. Winston or Bunyan), displays the first parameter
  logging: logger.debug.bind(logger)     // Alternative way to use custom logger, displays all messages

Check the authentication

If not logging happen! That may means that sequelize didn’t anything and is just hanging at the start! To test authentication and if the connection is working!

You can test with authenticate:

try {
  console.log('Gonna authenticate'); // <== to make sure console.log is working and not overrided!
  await sequelize.authenticate();
  console.log('Connection has been established successfully.');
} catch (error) {
  console.error('Unable to connect to the database:', error);

If you get no logging! And Gonna authenticate printed just ok! Then the process is hanging on authenticate. Which suggest a problem in authentication!

Check your credentials

Make sure you didn’t make any mistakes!

Check connectivity from psql or some other external client

  • If it doesn’t work! It suggest a problem with postgres server! Or some config!
  • If it does work! Then the problem is in nodejs! And your program!


As per the doc:

# One of the following:
$ npm install --save pg pg-hstore # Postgres
$ npm install --save mysql2
$ npm install --save mariadb
$ npm install --save sqlite3
$ npm install --save tedious # Microsoft SQL Server

One of the commands! Make sure you didn’t forget that!

Understanding what’s happening and debugging better! (code source logging)

The best way to debug! And really determine where the problem is happening! Is by adding logs to the source code itself! A fast way for me is to directly do that on node_modules. I opened git on sequelize repo! Made a search! Determined the place of sync, authenticate, query! All reside in sequelize.js! you can check here! One can CTRL + F to get to the methods > authenticate( [add the (]. Any way! You can do the same in node_modules! And start adding logs! You’ll know in which part the problem is happening! Which help you debug the problem!

The other way is to fork! And use your fork! And just work better!

But mmm! node_modules is a fast way! You can take a copy! too! To make sure you don’t loose your logs! Once you update! At the end clean by just removing the whole module! And reinstall! Or just reverse the logs creation (undo)! I find it an interesting way for debugging!

But it should throw an error

Normally it should! By seeing the code source you can know better! Normally an error should be thrown! But if a process get hanged! And no error is thrown! Then you can expect a result like this! Here that can be the driver missing! Also make sure console.log. Is working all right! And the last thing! MMM may be it’s a problem with nodejs itself (see the last section).

I’m using sequelize-typescript (not sequelize)

Very important to know! Sequelize-typescript is just a sequelize wrapper! That was there to add typescript support! It provide decorators and some features! Also From sequelize v5 ! Typescript is supported directly in sequelize! See here sequelize-typescript in the latest version! Turned to use too the native declaration types of sequelize!

As sequelize-typecript wrap sequelize! Make sure to verify sequelize documentation!

To note too there is some people shouting: Don’t use decorators! mmmm! mmm! And another mmmm!


If you are using sequelize-typescript make sure the version of sequelize-typescript and sequelize do match! As per there doc V5 of sequelize! I guess V6 too should do! And v1 for sequelize-typescript!

npm install [email protected] [email protected]

And don’t forget the typescript needed packages as per there doc!

(You can check and verify all those information in the doc itself)

Why to use sequelize-typescript ?

As already mentionned! Sequelize have native support for typescript starting from V5. As per here. So why use a wrapper above it! That use decorators too! (I’m not against decorators! Some are! As per here)

Ask yourself why ? Is there anything to sequelize-typescript! An important plus comparing to the native way ? If there is clear things! Please mention them in the comments! And i’ll update! This section!

And if not! Native can be way better! A dependency or many in less!

Project configuration


    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "moduleResolution": "node",
        "rootDir": "./src",
        "outDir": "./dist",
        "lib": [
        "declaration": true,
        "experimentalDecorators": true,
        "esModuleInterop": true
    "include": [
    "exclude": [

Those are the two important things to add.

"experimentalDecorators": true,
"esModuleInterop": true

But that should not be your problem! Otherwise the project will throw compile errors!


Node js VERSION (V14 HELL)

And yea! That can be the cause! You may already used to have it work all right before! And then in a new computer or environment! It’s not working any more!

Node version can be the problem! Node v15 and Node v14! That’s a known problem! I myself encountered once with knex.js and postgres (knex.js is a query builder)! So you can see that is related! In my story the code was working all right on my laptop and old vps that we deployed in! But then i deployed on a windows rdp! And mmm! Boom! Then i pulled my hair for some time! I reflected and checked! There was no change! And then i came to hey! I only updated nodejs! And later i found that other people encountered the same thing! In short! It all started on nodejs v14 (i call this v14 HELL)! You can check my answer about it here

And apparently the same problem is always there with nodejs v15!

In the question of this thread! We confirmed that! In my desktop all worked well! Nodejs v12! And with my friend computer! It didn’t! nodejs v14 and nodejs v15. Then i wanted to confirm! I installed nodejs v15 And caboom! BINGO! The execution just stop unexpectedly! No logging ! No error! In v12! All was working correctly! i had errors at first then i corrected them! And the server was up and running! And sequelize connected to the DB!

Here the executions

V12 and v13

I’m displaying v13! The same happen with v12!

enter image description here

nvm use v13
Now using node v13.14.0 (npm v6.14.4)
 [email protected]  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  npm run dev 

> [email protected] dev /home/coderhero/Documents/coderhero/Dev/projects/Fahima-ecommerce
> npx ts-node-dev src/server.ts

[INFO] 01:49:29 ts-node-dev ver. 1.0.0 (using ts-node ver. 9.0.0, typescript ver. 4.0.5)
config ::::
  username: 'fahima',
  password: '123456',
  database: 'fahimashop',
  host: 'localhost',
  dialect: 'postgres'
hi there ::::
Executing (default): SELECT 1+1 AS result
connection established
Executing (default): CREATE TABLE IF NOT EXISTS "Products" ("id"   SERIAL , "brand" TEXT, "price" DECIMAL, "description" VARCHAR(255), "imgUrl" VARCHAR(255), "category" VARCHAR(255), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'Products' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
server running http://localhost:8100
press CTRL+C to stop server, please :)

Cool working no problem

V14 and v15 execution


enter image description here

 [email protected]  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  node -v        
 [email protected]  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  npm run dev

> [email protected] dev /home/coderhero/Documents/coderhero/Dev/projects/Fahima-ecommerce
> npx ts-node-dev src/server.ts

[INFO] 02:07:35 ts-node-dev ver. 1.0.0 (using ts-node ver. 9.0.0, typescript ver. 4.0.5)
config ::::
  username: 'fahima',
  password: '123456',
  database: 'fahimashop',
  host: 'localhost',
  dialect: 'postgres'
hi there ::::
 [email protected]  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  

And opps! The program is exiting unexpectedly with no error output!


enter image description here

 [email protected]  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  nvm use v15    
Now using node v15.0.1 (npm v7.0.3)
 [email protected]  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  npm run dev

> [email protected] dev
> npx ts-node-dev src/server.ts

[INFO] 02:10:48 ts-node-dev ver. 1.0.0 (using ts-node ver. 9.0.0, typescript ver. 4.0.5)
config ::::
  username: 'fahima',
  password: '123456',
  database: 'fahimashop',
  host: 'localhost',
  dialect: 'postgres'
hi there ::::
 [email protected]  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  

And oppsii again! The program is exiting unexpectedly with no error output!

There is no difference too between v14 and v15! It’s V14 HELL.

In short

The V14 HELL is a known and very probable cause! There is a problem with pg module i guess! Something changed on v14 and caused this problem!

In short short! If nothing is making any sense! And if your same code was working before! First thing to do! Is to check with nodejs v13 or v12! That can save you from insanity! Who would say the version of nodejs and a new one will create such a problem!

What is this problem! What is V14 HELL in nodejs ?

If like me you like to know the details and what did happen !?

With node V14! Some breaking changes happened on the api! Also many things were changed! Including Openssl version!

For postgres! And pg module! The problem was as described in this comment per this thread:

The initial readyState (a private/undocumented API that

pg uses) of net.Socket seems to have changed from ‘closed’ to ‘open’ in Node 14.

It’s hard to fix with perfect backwards compatibility, but I think I have a patch that’s close enough.

And as per this PR!

You can see the changes in this diffing

In short as mentioned! The behavior for onReadySate changed for net.Socket ! And the implemented solution was to not use onReadyState at all!

And as per this

Connection now always calls connect on its stream when connect is called on it.

Check this line

In the older version the connect was called only if the socket is on closed state! readyState usage is eliminated!

You can understand! Depending on the implementation! Many things may or not be affected by those core changes!

Relevant node changes

And because i wanted to see where the change happen! Here you go and can check

One can check the log of changes too:

FIX (Postgres v14 HELL)

As per my answer here.

Upgrade pg driver to >=8.0.3! You can simply upgrade to latest!

npm install [email protected] --save

And you can check for our problem in question

Old version at v7

enter image description here

Updating to v8

enter image description here

Running again with node v15

enter image description here

Taraaaaa! And it worked awesomely!

And if you are not using postgres! And the problem was v14 HELL! Meaning You tested with v13. And it worked! Then try to upgrade your db driver to latest!

Why node v14 + exit and no logging error

Also to mention the breaking changes! Made pg make the process exit at connect() call. And that’s what made it exit! And logging was to be seen! In more detail for this! Here how it happened! Sequelize have the postgres dialect implementation! Which use pg! And pg client! create a connection! The connection have a connect event! When it connect it emit it! And because node v14 change the behavior to starting with open! The stream connection is skipped! And the stream is taken as connected! Where it is not! And the connect event is emitted directly! When that happen! The client either will call requestSsl() or startup() method of the connection object! And both will call this._stream.write. because the stream is not connected! An error happen! This error is not catch! Then the promise in sequelize driver! Will stay unresolved! And then the event loop get empty! Nodejs by default behavior just exit!

You can see the step through lines of code:

Why nodejs exit (unresolved promises)

Node exits without error and doesn’t await promise (Event callback)

what happens when a Promise never resolves?


If you don’t know what nvm is! Or you are not using nvm. Consider using it! As it’s a very interesting tool! Nvm is a node version management tool!

With nvm changing, debugging and testing to and with different version of nodejs! Is quick and a breeze! And so installing new versions of nodejs in parallel!

Note about sequelize.sync()

Do not use it for production! Or at all! (Most of ORM! And query builder (knex.js) use migrations).

From the doc

As shown above, sync({ force: true }) and sync({ alter: true }) can be destructive operations. Therefore, they are not recommended for production-level software. Instead, synchronization should be done with the advanced concept of Migrations, with the help of the Sequelize CLI.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top