Pulling down and running the latest Postgres images from Docker Hub now results in errors.
postgres_1_33777e03d7d8 | Error: Database is uninitialized and superuser password is not specified.
postgres_1_33777e03d7d8 | You must specify POSTGRES_PASSWORD for the superuser. Use
postgres_1_33777e03d7d8 | "-e POSTGRES_PASSWORD=password" to set it in "docker run".
postgres_1_33777e03d7d8 |
postgres_1_33777e03d7d8 | You may also use POSTGRES_HOST_AUTH_METHOD=trust to allow all connections
postgres_1_33777e03d7d8 | without a password. This is *not* recommended. See PostgreSQL
postgres_1_33777e03d7d8 | documentation about "trust":
postgres_1_33777e03d7d8 | https://www.postgresql.org/docs/current/auth-trust.html
Turns out there was a backwards compatibility breaking change added here.
Update your docker-compose.yml
or corresponding configuration with the POSTGRES_HOST_AUTH_METHOD
environment variable to revert back to previous behavior or implement a proper password.
image: library/postgres:11-alpine
environment:
POSTGRES_HOST_AUTH_METHOD: trust
Return home
Greetings, I am new to docker.
Situation
- I am following Quickstart guide of django which is stated in compose/django.md
- I’ve been following the walkthrough exactly the same, yet it seems like the guide has omitted postgres initialization.
- Mac OS Cattalina 10.15.3 / pipenv virtual environment / python 3.8
Problem
docker-compose up
fails with
db_1 | Error: Database is uninitialized and superuser password is not specified. db_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the db_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". db_1 | db_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all db_1 | connections without a password. This is *not* recommended. db_1 | db_1 | See PostgreSQL documentation about "trust": db_1 | https://www.postgresql.org/docs/current/auth-trust.html docker-docs-example_db_1 exited with code 1 web_1 | Watching for file changes with StatReloader web_1 | Performing system checks... web_1 | web_1 | System check identified no issues (0 silenced). web_1 | Exception in thread django-main-thread: web_1 | Traceback (most recent call last): web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection web_1 | self.connect() web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 195, in connect web_1 | self.connection = self.get_new_connection(conn_params) web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection web_1 | connection = Database.connect(**conn_params) web_1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, in connect web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) web_1 | psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known web_1 | web_1 | web_1 | The above exception was the direct cause of the following exception: web_1 | web_1 | Traceback (most recent call last): web_1 | File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner web_1 | self.run() web_1 | File "/usr/local/lib/python3.8/threading.py", line 870, in run web_1 | self._target(*self._args, **self._kwargs) web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 54, in wrapper web_1 | fn(*args, **kwargs) web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run web_1 | self.check_migrations() web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 453, in check_migrations web_1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__ web_1 | self.loader = MigrationLoader(self.connection) web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 49, in __init__ web_1 | self.build_graph() web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 212, in build_graph web_1 | self.applied_migrations = recorder.applied_migrations() web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 73, in applied_migrations web_1 | if self.has_table(): web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 56, in has_table web_1 | return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 256, in cursor web_1 | return self._cursor() web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 233, in _cursor web_1 | self.ensure_connection() web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection web_1 | self.connect() web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 89, in __exit__ web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection web_1 | self.connect() web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 195, in connect web_1 | self.connection = self.get_new_connection(conn_params) web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection web_1 | connection = Database.connect(**conn_params) web_1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, in connect web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) web_1 | django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known web_1 |
postgres_1 | Error: Database is uninitialized and superuser password is not specified.
postgres_1 | You must specify POSTGRES_PASSWORD to a non-empty value for the
postgres_1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgres_1 |
postgres_1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgres_1 | connections without a password. This is *not* recommended.
postgres_1 |
postgres_1 | See PostgreSQL documentation about "trust":
postgres_1 | [https://www.postgresql.org/docs/current/auth-trust.html](https://www.postgresql.org/docs/current/auth-trust.html)
react-node-docker-app_postgres_1 exited with code 1
version: '3'
services:
postgres:
image: 'postgres:latest'
nginx:
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '3050:80'
api:
build:
dockerfile: Dockerfile.dev
context: ./backend
volumes:
- /app/node_modules
- ./server:/app
environment:
- PGUSER=postgres
- PGHOST=postgres
- PGDATABASE=postgres
- PGPASSWORD=postgres_password
- PGPORT=5432
Where’s postgres_password stored? I thought the password was «postgres_password», but it seems to be a variable, but I have no idea where the variable is set. I don’t want to do «docker run POSTGRES_PASSWORD=»pass»», because I am using docker-compose run —build. Also how come the database is not initialized?
You can use the POSTGRES_HOST_AUTH_METHOD
environment property by making the following change to your docker-compose.yml
.
db:
image: postgres:9.6-alpine
environment:
POSTGRES_DB: "db"
POSTGRES_HOST_AUTH_METHOD: "trust"
The above will solve the error.
To avoid that you can specify the followings environments variables for postgres container on your docker-compose file.
POSTGRES_PASSWORD
This environment variable is normally required for you to use the PostgreSQL image. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable.
POSTGRES_DB
This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.
For more information about Environment Variables check:
https://hub.docker.com/_/postgres
It’s already mentioned in the interactive mode; how to run the container, if you don’t find it, use the following:
To allow all connections without a password use:
- docker run -e POSTGRES_HOST_AUTH_METHOD=trust postgres:9.6 (use the tag you need).
To specify postgres password for the superuser, use:
- docker run -e POSTGRES_PASSWORD=<your_password> postgres:9.6 (use the tag you need).
Tags:
Docker
Postgresql
Docker Compose
Related
Lets search for postgre images
E:practicesdockerpostgres>docker search postgres NAME DESCRIPTION STARS OFFICIAL AUTOMATED postgres The PostgreSQL object-relational database sy… 8009 [OK] sameersbn/postgresql 153 [OK] paintedfox/postgresql A docker image for running Postgresql. 77 [OK] bitnami/postgresql Bitnami PostgreSQL Docker Image 58 [OK] centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 44 postgrest/postgrest REST API for any Postgres database 31 arm32v7/postgres The PostgreSQL object-relational database sy… 26 circleci/postgres The PostgreSQL object-relational database sy… 22 wrouesnel/postgres_exporter Postgres metrics exporter for Prometheus. 22 centos/postgresql-10-centos7 PostgreSQL is an advanced Object-Relational … 17 centos/postgresql-94-centos7 PostgreSQL is an advanced Object-Relational … 16 schickling/postgres-backup-s3 Backup PostgresSQL to S3 (supports periodic … 15 [OK] postdock/postgres PostgreSQL server image, can work in master … 13 [OK] debezium/postgres PostgreSQL for use with Debezium change data… 12 [OK] clkao/postgres-plv8 Docker image for running PLV8 1.4 on Postgre… 12 [OK] prodrigestivill/postgres-backup-local Backup PostgresSQL to local filesystem with … 10 [OK] centos/postgresql-95-centos7 PostgreSQL is an advanced Object-Relational … 6 camptocamp/postgres Docker image for PostgreSQL including some e… 5 [OK] jgiannuzzi/postgres-bdr Docker image for PostgreSQL with BDR support 4 [OK] blacklabelops/postgres Postgres Image for Atlassian Applications 4 [OK] dcm4che/postgres-dcm4chee PostgreSQL for dcm4che-arc 5.x 3 [OK] ansibleplaybookbundle/postgresql-apb An APB which deploys RHSCL PostgreSQL 2 [OK] fredboat/postgres PostgreSQL 10.0 used in FredBoat's docker-co… 1 manageiq/postgresql Container with PostgreSQL and built on CentO… 0 [OK] cfcommunity/postgresql https://github.com/cloudfoundry-community/po… 0 E:practicesdockerpostgres>
Lets pull the latest image
E:practicesdockerpostgres>docker pull postgres Using default tag: latest latest: Pulling from library/postgres Digest: sha256:ec7cfff29672a2f676c11cc53ae7dafe63a57ccefc2b06ea423493227da29b9c Status: Image is up to date for postgres:latest docker.io/library/postgres:latest E:practicesdockerpostgres>
Using docker run
Run the docker image
E:practicesdockerpostgres>docker run -d --name pNode1 postgres:12 -e POSTGRES_PASSWORD=mysecretpassword -e POSTGRES_USER=postgress -e POSTGRES_DB=postgress -p 9432:5432 8c6ccb3f3089dd0d35ac4a437c42bab222b743dc2b0feefb11bd945c1801bfc3
Lets get the container details
E:practicesdockerpostgres>docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8c6ccb3f3089 postgres:12 "docker-entrypoint.s…" 13 seconds ago Exited (4294967295) 9 seconds ago pNode1
Lets view the logs to see if there is any error, and indeed there are errors, and it seems it is a bug in image
E:practicesdockerpostgres>docker logs 8c6ccb3f3089 Error: Database is uninitialized and superuser password is not specified. You must specify POSTGRES_PASSWORD to a non-empty value for the superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all connections without a password. This is *not* recommended. See PostgreSQL documentation about "trust": https://www.postgresql.org/docs/current/auth-trust.html E:practicesdockerpostgres>
Using Docker Compose
Since we cannot run docker images, let see if docker-compose works.
lets create db-data folder
docker-compose.yml
version: '3.6' services: pdb: image: postgres restart: always environment: POSTGRES_PASSWORD: postgrespassword ports: - 6543:5432 volumes: - ./db-data:/var/lib/postgresql/data adminer: image: adminer depends_on: - pdb restart: always ports: - 8080:8080 pgadmin: image: dpage/pgadmin4 depends_on: - pdb ports: - 7070:80 environment: PGADMIN_DEFAULT_EMAIL: pgadmin4@pgadmin.org PGADMIN_DEFAULT_PASSWORD: admin restart: unless-stopped
Lets run docker-compose
E:practicesdockerpostgres>docker-compose up -d Creating network "postgres_default" with the default driver Creating postgres_adminer_1 ... done Creating postgres_pdb_1 ... done Creating postgres_pgadmin_1 ... done E:practicesdockerpostgres>
Lets get container ids
E:practicesdockerpostgres>docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------ postgres_adminer_1 entrypoint.sh docker-php-e ... Up 0.0.0.0:8080->8080/tcp postgres_db_1 docker-entrypoint.sh postgres Up 5432/tcp
Lets view the logs
E:practicesdockerpostgres>docker-compose logs Attaching to postgres_adminer_1, postgres_db_1 db_1 | The files belonging to this database system will be owned by user "postgres". db_1 | This user must also own the server process. db_1 | db_1 | The database cluster will be initialized with locale "en_US.utf8". db_1 | The default database encoding has accordingly been set to "UTF8". db_1 | The default text search configuration will be set to "english". db_1 | db_1 | Data page checksums are disabled. db_1 | db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok db_1 | creating subdirectories ... ok db_1 | selecting dynamic shared memory implementation ... posix db_1 | selecting default max_connections ... 100 db_1 | selecting default shared_buffers ... 128MB db_1 | selecting default time zone ... Etc/UTC db_1 | creating configuration files ... ok db_1 | running bootstrap script ... ok db_1 | performing post-bootstrap initialization ... ok db_1 | syncing data to disk ... ok db_1 | db_1 | db_1 | Success. You can now start the database server using: db_1 | db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start db_1 | db_1 | initdb: warning: enabling "trust" authentication for local connections db_1 | You can change this by editing pg_hba.conf or using the option -A, or db_1 | --auth-local and --auth-host, the next time you run initdb. db_1 | waiting for server to start....2020-06-02 05:59:15.389 UTC [52] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit db_1 | 2020-06-02 05:59:15.399 UTC [52] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" db_1 | 2020-06-02 05:59:15.483 UTC [52] LOG: could not resolve "localhost": Name or service not known db_1 | 2020-06-02 05:59:15.483 UTC [52] LOG: disabling statistics collector for lack of working socket db_1 | 2020-06-02 05:59:15.483 UTC [52] WARNING: autovacuum not started because of misconfiguration db_1 | 2020-06-02 05:59:15.483 UTC [52] HINT: Enable the "track_counts" option. db_1 | 2020-06-02 05:59:15.495 UTC [53] LOG: database system was shut down at 2020-06-02 05:59:10 UTC db_1 | 2020-06-02 05:59:15.516 UTC [52] LOG: database system is ready to accept connections db_1 | done db_1 | server started db_1 | db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* db_1 | db_1 | 2020-06-02 05:59:15.582 UTC [52] LOG: received fast shutdown request db_1 | waiting for server to shut down....2020-06-02 05:59:15.589 UTC [52] LOG: aborting any active transactions db_1 | 2020-06-02 05:59:15.591 UTC [52] LOG: background worker "logical replication launcher" (PID 57) exited with exit code 1 db_1 | 2020-06-02 05:59:15.591 UTC [54] LOG: shutting down db_1 | 2020-06-02 05:59:15.655 UTC [52] LOG: database system is shut down db_1 | done db_1 | server stopped db_1 | db_1 | PostgreSQL init process complete; ready for start up. db_1 | db_1 | 2020-06-02 05:59:15.713 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit db_1 | 2020-06-02 05:59:15.713 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 db_1 | 2020-06-02 05:59:15.713 UTC [1] LOG: listening on IPv6 address "::", port 5432 db_1 | 2020-06-02 05:59:15.722 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" db_1 | 2020-06-02 05:59:15.804 UTC [1] LOG: could not resolve "localhost": Name or service not known db_1 | 2020-06-02 05:59:15.804 UTC [1] LOG: disabling statistics collector for lack of working socket db_1 | 2020-06-02 05:59:15.804 UTC [1] WARNING: autovacuum not started because of misconfiguration db_1 | 2020-06-02 05:59:15.804 UTC [1] HINT: Enable the "track_counts" option. db_1 | 2020-06-02 05:59:15.817 UTC [59] LOG: database system was shut down at 2020-06-02 05:59:15 UTC db_1 | 2020-06-02 05:59:15.841 UTC [1] LOG: database system is ready to accept connections adminer_1 | [Tue Jun 2 05:59:07 2020] PHP 7.4.6 Development Server (http://[::]:8080) started E:practicesdockerpostgres>
Connecting to Postgres DB by getting into container
Lets connect to container
E:practicesdockerpostgres>docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------ postgres_adminer_1 entrypoint.sh docker-php-e ... Up 0.0.0.0:8080->8080/tcp postgres_db_1 docker-entrypoint.sh postgres Up 5432/tcp E:practicesdockerpostgres>docker exec -it postgres_db_1 bash root@8f2771dd82ed:/#
change user
root@8f2771dd82ed:/# su postgres postgres@8f2771dd82ed:/$
list the dbs
postgres=# help You are using psql, the command-line interface to PostgreSQL. Type: copyright for distribution terms h for help with SQL commands ? for help with psql commands g or terminate with semicolon to execute query q to quit postgres=#
postgres=# l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) postgres=#
Connecting to Postgres DB Adminer
lets connect to Aminer http://localhost:8080/
Connect To Postgres DB from local terminal
psql -h <host> -p <port> -U <user> -d <db>
C:Usersnadeem>psql -h localhost -p 9090 -U postgres -d postgres Password for user postgres: psql (12.1, server 12.3 (Debian 12.3-1.pgdg100+1)) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. postgres=# l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows) postgres=#
Connect to Postgres from DBeaver
Creating Database
postgres=# CREATE DATABASE testDb; CREATE DATABASE postgres=#
Create User
postgres-# q postgres@8f2771dd82ed:/$ createuser testuser -P --createdb Enter password for new role: Enter it again: postgres@8f2771dd82ed:/$
postgres=# grant all privileges on database testdb to testuser; GRANT postgres=#
Doing purely via psql
CREATE DATABASE yourdbname; CREATE USER youruser WITH ENCRYPTED PASSWORD 'yourpass'; GRANT ALL PRIVILEGES ON DATABASE yourdbname TO youruser;
- connect
- outside
- Postgresql
- running
- Terminal