Postgres: Cannot setup password

Created on 19 Oct 2017  路  4Comments  路  Source: docker-library/postgres

Here is the docker info:

Containers: 1
 Running: 1
 Paused: 0
 Stopped: 0
Images: 3
Server Version: 17.09.0-ce
Storage Driver: overlay2
 Backing Filesystem: extfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 06b9cb35161009dcb7123345749fef02f7cea8e0
runc version: 3f2f8b84a77f73d38244dd690525642a72156c64
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 4.9.0-4-amd64
Operating System: Debian GNU/Linux 9 (stretch)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.699GiB
Name: debian
ID: FPDX:4NTE:NLZZ:7XRM:AWEQ:5FTA:F35N:CV5N:ATDF:Y5M6:GOFP:OULM
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

I run Postgres instance with the following command:

$ sudo docker run --name pg -v postgres_data:/var/lib/postgresql/data -p 5432:5432 -d postgres

And then, log into the database and setup password for 'postgres' user:

$ psql -U postgres -d postgres -h localhost
psql (9.6.4, server 10.0)
WARNING: psql major version 9.6, server major version 10.
         Some psql features might not work.
Type "help" for help.

postgres=# \password postgres
Enter new password: 
Enter it again: 
postgres=# \q
$ psql -U postgres -d postgres -h localhost
psql (9.6.4, server 10.0)
WARNING: psql major version 9.6, server major version 10.
         Some psql features might not work.
Type "help" for help.

postgres=#

It doesn't ask me for the password. I also try to create a new user and new database and log in again. It doesn't work either. Everything else works fine until now. And Postgres image version is 10.0.

Most helpful comment

It seems I must've missed this issue when it was first filled. The reason for no password being required is the config in pg_hba.cnf. If you don't use the POSTGRES_PASSWORD environment variable then the entrypoint script sets up pg_hba.cnf to trust on any connection, so no password is required and you'll see the warning in the logs. You need to update your pg_hba.cnf file or start a new container with an empty database directory with the environment variable set.

Also, even if you use the env var, any connection via localhost of the container will be trusted (which is normally limited to something like docker exec). The password will still apply to the connection from the Docker host localhost via a port mapping on docker run: -p 5432:5432, since it is external to the container's localhost and would be accessing its eth0 via the Docker bridge.

As for @twalk4821 and @ddwilly, I am not sure if this is the same as your problem. You may also want to look at https://github.com/docker-library/postgres/issues/203#issuecomment-255200501 if you started the container and then tried adding a password later.

As far as I can tell the original problem should be solved, so I'll close the issue.

All 4 comments

I have this same issue. I think it's because of the way the config file is set up. Looking for answers...

Hi Guys..I have the same issue too. I am unable to set a password for user postgres. Also tested it with a new user and tried to create a password "Same issue" It lets me in without a password. I am wondering if I am doing something silly as so many people have downloaded this image.

It seems I must've missed this issue when it was first filled. The reason for no password being required is the config in pg_hba.cnf. If you don't use the POSTGRES_PASSWORD environment variable then the entrypoint script sets up pg_hba.cnf to trust on any connection, so no password is required and you'll see the warning in the logs. You need to update your pg_hba.cnf file or start a new container with an empty database directory with the environment variable set.

Also, even if you use the env var, any connection via localhost of the container will be trusted (which is normally limited to something like docker exec). The password will still apply to the connection from the Docker host localhost via a port mapping on docker run: -p 5432:5432, since it is external to the container's localhost and would be accessing its eth0 via the Docker bridge.

As for @twalk4821 and @ddwilly, I am not sure if this is the same as your problem. You may also want to look at https://github.com/docker-library/postgres/issues/203#issuecomment-255200501 if you started the container and then tried adding a password later.

As far as I can tell the original problem should be solved, so I'll close the issue.

if POSTGRES_PASSWORD was not set during container creation you can modify container /var/lib/postgresql/data/pg_hba.conf from

host all all all trust

to

host all all all md5
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndriiOmelianenko picture AndriiOmelianenko  路  4Comments

weeger picture weeger  路  4Comments

andrewvo148 picture andrewvo148  路  3Comments

tjamet picture tjamet  路  4Comments

Enelar picture Enelar  路  4Comments