Connecting MikroORM migration with Postgresql? npx command is failing. What am I missing?

1.4k views Asked by At

I'm having a similar issue as described here. Following Ben Awad's YouTube tutorial: see here for where I am in the tutorial.

Goal: Run npx mikro-orm migration:create to create a Mikro ORM migration as shown in the tutorial.

What I did so far: I was able to setup Postgres 13 and connect via the psql CLI (SQL Shell) and the pgAdmin tool. But when I run the npx mikro-orm migration:create in my VS Code terminal, I get:

error: password authentication failed for user "postgres"
        at Parser.parseErrorMessage (C:\EJdesktop\Web Dev\playground\reddit-server\node_modules\pg-protocol\src\parser.ts:357:11)
        at Parser.handlePacket (C:\EJdesktop\Web Dev\playground\reddit-server\node_modules\pg-protocol\src\parser.ts:186:21)
        at Parser.parse (C:\EJdesktop\Web Dev\playground\reddit-server\node_modules\pg-protocol\src\parser.ts:101:30)
        at Socket.<anonymous> (C:\EJdesktop\Web Dev\playground\reddit-server\node_modules\pg-protocol\src\index.ts:7:48)
        at Socket.emit (events.js:314:20)
        at Socket.EventEmitter.emit (domain.js:486:12)
        at addChunk (_stream_readable.js:307:12)
        at readableAddChunk (_stream_readable.js:282:9)
        at Socket.Readable.push (_stream_readable.js:221:10)
        at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
      length: 163,
      severity: 'FATAL',
      code: '28P01',
      detail: undefined,
      hint: undefined,
      position: undefined,
      internalPosition: undefined,
      internalQuery: undefined,
      where: undefined,
      schema: undefined,
      table: undefined,
      column: undefined,
      dataType: undefined,
      constraint: undefined,
      file: 'd:\\pginstaller.auto\\postgres.windows-x64\\src\\backend\\libpq\\auth.c',
      line: '336',
      routine: 'auth_failed'
    }

Here is my mikro-orm.config.ts file:

import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import { MikroORM } from "@mikro-orm/core";
import path from "path";


export default {
        migrations: {
            //join file paths
            path: path.join(__dirname, './migrations'), // path to the folder with migrations
            pattern: /^[\w-]+\d+\.[tj]s$/, // regex pattern for the migration files
        },
        entities: [Post],
        dbName: 'postgres',
        type: 'postgresql',
        debug: !__prod__
} as Parameters<typeof MikroORM.init>[0]

My pg_hba.conf (for Postgres) originally showed as follows:

local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

Tried changing it to add 3 more lines to match the example referenced above:

local   all             all                                     peer
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

I keep getting the same error.

Let me know if I can clarify anything further. Any advice?

1

There are 1 answers

0
Ruslan On
  1. In pg_hba.conf file change postgres user's method to md5. Keep the trusted ones as they are.

  2. Go to postgresql.conf file. It's in the same directory as the pg_hba.conf. Change the default port to 5432. Mine was set to 5433 for some reason.

  3. Run this command: sudo service postgresql restart. Go to your config object and add user and password properties and indicate "postgres" as user, and whatever password you set for postgres user.

  4. Now run npx mikro-migration:create.

Hopefully this will help.