Problem connecting to postgres docker container using npm run develop command, strapi using windows authentication

System Information
  • Strapi Version: 4.9.1
  • Operating System: Win 10
  • Database: postgres:14.7-alpine3.17 (docker container)
  • Node Version: 18.12.2
  • NPM Version: 9.6.2
  • Yarn Version: na

I have problems starting a new strapi backend using the npm run develop command.

I am starting a postgres in a container like so:

docker run --name=strapi -p 5432:5432 -e POSTGRES_DB=planrec -e POSTGRES_USER=strapi -e POSTGRES_PASSWORD=strapi -d postgres:14.7-alpine3.17

The container is already running when i run npm run develop. This is the output from npm:

> planrec@0.1.0 develop
> strapi develop

Starting the compilation for TypeScript files in C:\code\planrec
Building your admin UI with development configuration...
Admin UI built successfully
[2023-04-13 08:16:12.496] debug: ⛔️ Server wasn't able to start properly.
[2023-04-13 08:16:12.499] error: password authentication failed for user "benxa"
error: password authentication failed for user "benxa"
    at Parser.parseErrorMessage (C:\code\planrec\node_modules\pg-protocol\dist\parser.js:287:98)
    at Parser.handlePacket (C:\code\planrec\node_modules\pg-protocol\dist\parser.js:126:29)
    at Parser.parse (C:\code\planrec\node_modules\pg-protocol\dist\parser.js:39:38)
    at Socket.<anonymous> (C:\code\planrec\node_modules\pg-protocol\dist\index.js:11:42)
    at Socket.emit (node:events:513:28)
    at Socket.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

benxa is my windows username on this machine. I know that postgres will default to windows username for authentication. What I find odd is that it appears to be ignoring all the values in .env:

HOST=0.0.0.0
PORT=1337
APP_KEYS=NJEKqyvxhRmDhRnoMlARrQ==,jc4fpMWmorT3rpjSRsdxsg==,f5SWsMiILKJzS8C56YOfmw==,YGdCXBZqT2ndRM7ZSrY4DQ==
API_TOKEN_SALT=OFRYyQTxJfu6oJHmw9s6iQ==
ADMIN_JWT_SECRET=ncZ4NxWv9RwtvVaI6doxAg==
TRANSFER_TOKEN_SALT=QWetO67oSvn7dEh6e9KPnA==
# Database
DATABASE_CLIENT=postgres
DATABASE_HOST=127.0.01
DATABASE_PORT=5432
DATABASE_NAME=planrec
DATABASE_USERNAME=strapi
DATABASE_PASSWORD=strapi
DATABASE_SSL=false
DATABASE_URL="postgresql://strapi:strapi@localhost:5432/planrec?sslmode=disable"

I added DATABASE_URL on my last try to connect.

Issue seems similar to this one which was unanswered:
https://forum.strapi.io/t/pg-lib-incorreclty-using-windows-username-to-connect-to-postgres/13805

Any thoughts appreciated.

What does your database.js look like ?.

Hi Eventyret,

The problem is partially solved. I dont seem to be able to pass the connection string using .env variable but when i hardcode the string into the connection object it works fine. So this works:

        connection: {
        connectionString: 'postgresql://strapi:strapi@localhost/plnr?sslmode=disable',
        //connectionString: env('DATABASE_URL'),
        host: env('DATABASE_HOST', 'localhost'),
        port: env.int('DATABASE_PORT', 5432),
        database: env('DATABASE_NAME', 'strapi'),
        user: env('DATABASE_USERNAME', 'strapi'),
        password: env('DATABASE_PASSWORD', 'strapi'),
        ssl: env.bool('DATABASE_SSL', false) && {
          key: env('DATABASE_SSL_KEY', undefined),
          cert: env('DATABASE_SSL_CERT', undefined),
          ca: env('DATABASE_SSL_CA', undefined),
          capath: env('DATABASE_SSL_CAPATH', undefined),
          cipher: env('DATABASE_SSL_CIPHER', undefined),
          rejectUnauthorized: env.bool(
            'DATABASE_SSL_REJECT_UNAUTHORIZED',
            true
          ),
        },
        schema: env('DATABASE_SCHEMA', 'public'),
      },

But string in the env file like this:

...
# Database
DATABASE_URL=postgresql://strapi:strapi@localhost/plnr?sslmode=disable
DATABASE_CLIENT=postgres
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=plnr
DATABASE_USERNAME=strapi
DATABASE_PASSWORD=strapi
DATABASE_SSL=false
...

with connectionString: env('DATABASE_URL'), in connection object does not work. Which is fine for development purposes but will break our preferred deployment process.

Thanks for your attention.