I am using django-cookiecutter template and for the second time for new project. And it fails to connect to postgres with following error:
postgres_1 | 2018-04-30 14:54:09.747 UTC [1] LOG: database system is ready to accept connections
postgres_1 | 2018-04-30 14:54:10.029 UTC [28] FATAL: password authentication failed for user "IViLGLIEWLBDGBnsAuoOEhtFaKrqKxfX"
postgres_1 | 2018-04-30 14:54:10.029 UTC [28] DETAIL: Role "IViLGLIEWLBDGBnsAuoOEhtFaKrqKxfX" does not exist.
postgres_1 | Connection matched pg_hba.conf line 95: "host all all all md5"
django_1 | PostgreSQL is unavailable (sleeping)...
One of the maintainers explained this:
the thing is, every time you bootstrap the project POSTGRES_USER and POSTGRES_PASSWORD get reset to a newly-generated random values
I tried to remove all docker containers but no success any idea how am i able to solve this? I dont have the old credentials to replace them.
With the help of https://github.com/webyneter (contributor to django-cookiecutter)
The solution is following:
To see existing volumes: docker volume ls
To remove the respective volumes docker volume rm <your project_slug>_postgres_backup_local <your project_slug>_postgres_data_local
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I am setting up a local Postgres database on Docker with the postgres:14-alpine image, and running database migrations on it with golang-migrate, when I got the following error message after running the migrate tool:
error: pq: role "root" does not exist
I was running the following commands:
$ docker run --name postgres14 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=pass -d postgres:14-alpine
$ docker exec -it postgres14 createdb --user=root --owner=root demodb
$ migrate -path db/migrations -database postgresql://root:pass#localhost:5432/demodb?sslmode=disable --verbose up
These commands can also be viewed in this Makefile, and the full codebase can be found in this repository.
Here are the logs from the Postgres 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 ... UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
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....2022-10-15 09:56:41.209 UTC [36] LOG: starting PostgreSQL 14.5 on x86_64-pc-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit
2022-10-15 09:56:41.211 UTC [36] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-10-15 09:56:41.217 UTC [37] LOG: database system was shut down at 2022-10-15 09:56:41 UTC
2022-10-15 09:56:41.220 UTC [36] LOG: database system is ready to accept connections
done
server started
CREATE DATABASE
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
waiting for server to shut down...2022-10-15 09:56:41.422 UTC [36] LOG: received fast shutdown request
.2022-10-15 09:56:41.423 UTC [36] LOG: aborting any active transactions
2022-10-15 09:56:41.423 UTC [36] LOG: background worker "logical replication launcher" (PID 43) exited with exit code 1
2022-10-15 09:56:41.424 UTC [38] LOG: shutting down
2022-10-15 09:56:41.434 UTC [36] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
What should I do to configure the root role correctly?
The docker image docs specify that POSTGRES_USER environment variable defaults to postgres if not set, try using that instead of root or drop the container and build it again using the correct environment variable
once you are inside the psql shell you can create a user with
CREATE USER username WITH PASSWORD 'your_password';
then to grant the user access on a specific database:
GRANT ALL PRIVILEGES ON DATABASE demodb TO username;
once that is done you can use the user in the connection string in make file
Turns out the Postgres server that was installed and setup on my OS by Hombrew was using the same port, which clashed with the requests made to the containerized database under the same port number.
This issue can be solved by either using a different port number for the containerized database, or by shutting down the database on the OS.
We had a problem with our hard drive which contains the data folder for postgres database we repaired that hard and move the data folder to new hard drive now when we try to start our db container we get following error.
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | FATAL: bogus data in lock file "postmaster.pid": ""
I tried to remove the postmaster.pid file but then I get the following error,
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | LOG: database system was interrupted; last known up at 2020-04-07 18:03:04 UTC
db_1 | LOG: invalid primary checkpoint record
db_1 | LOG: invalid secondary checkpoint record
db_1 | PANIC: could not locate a valid checkpoint record
db_1 | LOG: startup process (PID 28) was terminated by signal 6: Aborted
db_1 | LOG: aborting startup due to startup process failure
app_db_1 exited with code 1
we really need the data and we do not have any backup what we should do my docker-compose.yml contains the following contents for db container,
db:
image: postgres:9.4
volumes:
- /mnt/hdd4/postgresql/data:/var/lib/postgresql/data
- ./tmp:/tmp/docker
ports:
- "5432"
Please let me know if extra information is required.
It seems like important parts of your database got destroyed by the disk problem. If you had a backup, that would be the time to restore it. If not, you had best hire a PostgreSQL consultant who is skilled with salvaging data from a corrupted database like this.
I am trying to set up a postgres container to start and run initializing the creation of a table. I've succeeded with the straight image from docker but now that I am trying to extend the image a little to create tables when it's produced and I can't get it running. Based off what I've read here How to create User/Database in script for Docker Postgres, this is what I have:
Dockerfile:
FROM library/postgres
COPY init.sql /docker-entrypoint-initdb.d/
init.sql:
CREATE TABLE incident_disposition (
incident_disposition_code VARCHAR,
incident_disposition_code_description VARCHAR
);
From what I understand, FROM library . . . pulls the postgres image from docker hub and the COPY pushes my init.sql script into the entry point so there is no need for a big dockerfile correct?
I then build the image no issue:
Build
docker build -t my_postgres_image .
But when I run I get the issues:
Run
docker run --name testing my_postgres_image --publish 8000:8080 --detach -e POSTGRES_PASSWORD=postgres -d postgres
Errors from logs
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
Attempt from comment:
docker container logs testing
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
syncing data to disk ... ok
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
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.
waiting for server to start....2020-03-26 14:06:51.064 UTC [46] 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-03-26 14:06:51.072 UTC [46] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-03-26 14:06:51.108 UTC [47] LOG: database system was shut down at 2020-03-26 14:06:50 UTC
2020-03-26 14:06:51.119 UTC [46] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
CREATE TABLE
2020-03-26 14:06:51.231 UTC [46] LOG: received fast shutdown request
waiting for server to shut down....2020-03-26 14:06:51.232 UTC [46] LOG: aborting any active transactions
2020-03-26 14:06:51.233 UTC [46] LOG: background worker "logical replication launcher" (PID 53) exited with exit code 1
2020-03-26 14:06:51.234 UTC [48] LOG: shutting down
2020-03-26 14:06:51.290 UTC [46] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
2020-03-26 14:06:51.345 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-03-26 14:06:51.345 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2020-03-26 14:06:51.345 UTC [1] LOG: listening on IPv6 address "::", port 5432
2020-03-26 14:06:51.361 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-03-26 14:06:51.387 UTC [64] LOG: database system was shut down at 2020-03-26 14:06:51 UTC
2020-03-26 14:06:51.398 UTC [1] LOG: database system is ready to accept connections
2020-03-26 14:07:27.715 UTC [72] ERROR: relation "incident_disposition" does not exist at character 15
2020-03-26 14:07:27.715 UTC [72] STATEMENT: select * from incident_disposition;
In addition to comments
Due to recent docker image's updates postgres images do not allow to connect to DB without a password from anywhere. So you need to specify username/password
docker run -p 8000:8080 -e POSTGRES_PASSWORD=postgres --name testing -d my_postgres_image
Or if you still don't want to use password, you can just set POSTGRES_HOST_AUTH_METHOD=trust environment variable:
docker run -p 8000:8080 -e POSTGRES_HOST_AUTH_METHOD=trust --name testing -d my_postgres_image
It is a typical Initialization scripts issue.
You can file the full explaination in postgresql docker page. https://hub.docker.com/_/postgres
Here is the brief intro:
1. One common problem is that if one of your /docker-entrypoint-initdb.d scripts fails (which will cause the entrypoint script to exit) and your orchestrator restarts the container with the already initialized data directory, it will not continue on with your scripts.
note:
in your case, you may need clean the historical docker containers(stopped) by
step 1: docker ps |grep
step 2: docker rm -f -v
Or if you are using docker-compose, the historical orchestrator could be easily removed by docker-compose down -v.
I'd like to try out the quick start example at https://github.com/wrouesnel/postgres_exporter on a MacOS host device. In one terminal, I run a postgres image in interactive mode on the host network:
> docker run --network=host -it --rm --env POSTGRES_PASSWORD=password postgres
Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
8f91359f1fff: Pull complete
c6115f5efcde: Pull complete
28a9c19d8188: Pull complete
2da4beb7be31: Pull complete
fb9ca792da89: Pull complete
cedc20991511: Pull complete
b866c2f2559e: Pull complete
5d459cf6645c: Pull complete
cf66247ad881: Pull complete
35e29440d9da: Pull complete
362779251360: Pull complete
fb82d778d08d: Pull complete
b1f8d21ff25d: Pull complete
6d49eb0e8dd0: Pull complete
Digest: sha256:be456a40361cd836e0e1b35fc4d872e20e138f214c93138425169c4a2dfe1b0e
Status: Downloaded newer image for postgres:latest
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 default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Etc/UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
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.
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
waiting for server to start....2019-10-03 19:06:10.222 UTC [42] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-10-03 19:06:10.235 UTC [43] LOG: database system was shut down at 2019-10-03 19:06:10 UTC
2019-10-03 19:06:10.239 UTC [42] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
waiting for server to shut down....2019-10-03 19:06:10.320 UTC [42] LOG: received fast shutdown request
2019-10-03 19:06:10.322 UTC [42] LOG: aborting any active transactions
2019-10-03 19:06:10.325 UTC [42] LOG: background worker "logical replication launcher" (PID 49) exited with exit code 1
2019-10-03 19:06:10.325 UTC [44] LOG: shutting down
2019-10-03 19:06:10.338 UTC [42] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
2019-10-03 19:06:10.436 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2019-10-03 19:06:10.438 UTC [1] LOG: listening on IPv6 address "::", port 5432
2019-10-03 19:06:10.440 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2019-10-03 19:06:10.450 UTC [51] LOG: database system was shut down at 2019-10-03 19:06:10 UTC
2019-10-03 19:06:10.453 UTC [1] LOG: database system is ready to accept connections
while in another terminal, I run postgres_exporter and connect it to that database:
> docker run --net=host --env DATA_SOURCE_NAME="postgresql://postgres:password#localhost:5432/postgres?sslmode=disable" wrouesnel/postgres_exporter
Unable to find image 'wrouesnel/postgres_exporter:latest' locally
latest: Pulling from wrouesnel/postgres_exporter
0d6d2d2516f9: Pull complete
e9d7b571ef5e: Pull complete
Digest: sha256:aeea975f0efeacb49c170f0f7c4a4000d3f0099cc33437aedd3f276e628cde1c
Status: Downloaded newer image for wrouesnel/postgres_exporter:latest
time="2019-10-03T19:09:20Z" level=info msg="Established new database connection to \"localhost:5432\"." source="postgres_exporter.go:778"
time="2019-10-03T19:09:20Z" level=info msg="Semantic Version Changed on \"localhost:5432\": 0.0.0 -> 11.5.0" source="postgres_exporter.go:1238"
time="2019-10-03T19:09:20Z" level=info msg="Starting Server: :9187" source="postgres_exporter.go:1459"
As I understand it, I should be able to go to localhost:9187 in my browser and see the exported metrics. What I get, however, is that the connection gets refused:
> curl http://localhost:9187
curl: (7) Failed to connect to localhost port 9187: Connection refused
What I suspect is that this is because, as documented at https://docs.docker.com/network/host/,
The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.
Is this what is causing the issue and if so, how can I adapt this quickstart example to work using Docker Desktop for Mac?
A little late, but maybe for someone in the future. I had the same problem on Docker Desktop for Windows. For it to work I used the following command:
docker run -p 9187:9187 -e DATA_SOURCE_NAME="postgresql://postgres:password#localhost:5432/postgres?sslmode=disable" wrouesnel/postgres_exporter
I was having issues connecting to my PG database in my compose file. I finally eliminated all other factors and made this bare-bones compose file:
version: '2.1'
services:
database:
image: postgres:9.5.6
environment:
POSTGRES_PASSWORD: secretpass
ports:
- 5432:5432
I then started it up with this command:
docker-compose --file docker-compose.pg.yml up --build
and it seemed to start ok:
Starting app_database_1 ...
Starting app_database_1 ... done
Attaching to app_database_1
database_1 | LOG: database system was shut down at 2017-09-22 18:48:55 UTC
database_1 | LOG: MultiXact member wraparound protections are now enabled
database_1 | LOG: database system is ready to accept connections
database_1 | LOG: autovacuum launcher started
Then from another shell, tried to connect with this command:
psql postgresql://postgres:secretpass#0.0.0.0
And got this output on my first shell where the compose was run:
database_1 | FATAL: password authentication failed for user "postgres"
database_1 | DETAIL: Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5"
I have also tried to connect from another container running
Rails defined in the compose file and get the same error. I always end up with an authentication error. What am I missing?