Robin G
Robin G

Reputation: 387

Postgres database does not connect, .sync() does not resolve. Sequelize, PostgreSQL, Node

Recently, I got a new laptop and I am set up my development environment on there. Also, I copied a project I worked on on my old laptop and wanted to continue working on it on my new laptop. Nothing weird here, I would think. The server-side code in this case.

So I started with installing all the apps and programs, cloned my GitHub repo, booted up a docker container with the following command:

docker run -d -p 5432:5432 --name ulti-mate -e POSTGRES_PASSWORD=secret postgres

and ran npm install. I connected to the database using postico, just to have a little visual feedback, which connected instantly. Then started the server up using nodemon index.js and it seemed to start. Only I was missing one thing: Normally the server console logs Database connected and runs a bunch of insert queries. Went to Postico, nothing.

I've been going over the code of my database:

const Sequelize = require('sequelize');
const databaseUrl =
  process.env.DATABASE_URL ||
  'postgres://postgres:secret@localhost:5432/postgres';
const db = new Sequelize(databaseUrl);

async function syncDB() {
  try {
    console.log('Before sync');
    await db.sync({ force: false });
    console.log('Database connected');
  } catch (error) {
    console.error('error synching database', error);
  }
}
syncDB();

and I noticed that it runs until it hits db.sync(). Before sync logs consistently. That's where it stops. It doesn't resolve at all. I tried assigning it to a variable, but nothing. For example, this does not log:

const a = await db.sync({force: false});
console.log("a:", a);

The weird thing is that it worked before on my old machine, so the problem can't be in the code. It must have something to do with my new development environment. I tried installing different versions of sequelize and pg in the repo, didn't help. I tried reinstalling postgresql with homebrew, but it's up to date.

If anyone has an idea what might be going wrong or something I might try to fix this issue, it would be greatly appreciated, because I'm slowly going mad.

Upvotes: 0

Views: 809

Answers (1)

Robin G
Robin G

Reputation: 387

I figured out the problem. I was running Node v14.4.0 (latest version at the moment). Downgrading to the latest LTS version (v12.18.1) fixed the issue. I'm not sure if this is a known issue, but I opened a ticket on the sequelize Repo.

Upvotes: 2

Related Questions