skip to Main Content

I’m doing the same steps as described in: https://medium.com/better-programming/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7

but when I try to connect to my postgres (with password mysecretpassword) from Intellij I get the following error:

The specified database user/password combination is rejected: [28P01] FATAL: password authentication failed for user "postgres"

enter image description here

Of course I can connect to my db from cmd command:

$ psql -h localhost -p 5432 -U postgres -W                       Password for user postgres:                       
psql (9.5.5, server 10.3 (Debian 10.3-1.pgdg90+1))                       WARNING: psql major version 9.5, server major version 10.                                Some psql features might not work.                       
Type "help" for help.                                               postgres=# l

My container is up:
enter image description here

What is going on? I don’t have any idea… I use postgres driver 42.2.5 in Intellij

Logs from container:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
syncing data to disk ... ok


Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....2020-09-30 11:17:42.613 UTC [45] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-09-30 11:17:42.618 UTC [45] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-09-30 11:17:42.642 UTC [46] LOG:  database system was shut down at 2020-09-30 11:17:42 UTC
2020-09-30 11:17:42.649 UTC [45] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

2020-09-30 11:17:42.700 UTC [45] LOG:  received fast shutdown request
waiting for server to shut down....2020-09-30 11:17:42.705 UTC [45] LOG:  aborting any active transactions
2020-09-30 11:17:42.708 UTC [45] LOG:  background worker "logical replication launcher" (PID 52) exited with exit code 1
2020-09-30 11:17:42.708 UTC [47] LOG:  shutting down
2020-09-30 11:17:42.737 UTC [45] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2020-09-30 11:17:42.836 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-09-30 11:17:42.836 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-09-30 11:17:42.836 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2020-09-30 11:17:42.845 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-09-30 11:17:42.881 UTC [54] LOG:  database system was shut down at 2020-09-30 11:17:42 UTC
2020-09-30 11:17:42.888 UTC [1] LOG:  database system is ready to accept connections

3

Answers


  1. create dir for volume: C:Users{user}dockerpostgres_datadata

    Connect to Postgres on Windows:

    docker run -p 5432:5432 --name postgres -v C:Users{user}dockerpostgres_datadata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=root -d postgres:11.3 
    

    Connect to Postgres from CMD and create DB (only after first creation container on empty volume):

    docker ps
      
    

    get the output:
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    e626f4c7b958 postgres:11.3 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5432->5432/tcp postgres

    Run:

    docker exec -it {CONTAINER ID} bash
    psql -h localhost -U postgres
    psql -U postgres
    

    Connect from Intellij with PASSWORD=root:

    enter image description here

    Login or Signup to reply.
  2. It seems that you have running postgresql instance on 5432 port. Check what is running on port 5432:

    sudo lsof -i :5432
    

    If there are any records, you could manually kill the processes that are using this port (man kill) and re-run docker container. In my case there was another local instance of postgresql running on this port, so the following helps me:

    sudo pkill -u postgres
    

    Or (more preferable) just specify a different port mapping for your docker postgresql (for e.g. 5433):

    docker run -p 5433:5432 ...other_your_flags... postgres:latest
    
    Login or Signup to reply.
  3. try to remove your PostgreSQL the one you have installed locally, if it exists. Because my problem was precisely that the idea did not understand where to connect, to the docker survey or to that installed database on my computer

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search