"error: pq: role "root" does not exist" when running pq with Postgres for Docker

huangapple go评论87阅读模式
英文:

"error: pq: role "root" does not exist" when running pq with Postgres for Docker

问题

我正在使用postgres:14-alpine镜像在Docker上设置本地Postgres数据库,并使用golang-migrate在其中运行数据库迁移时,运行migrate工具后出现以下错误消息:

error: pq: role "root" does not exist

我运行了以下命令:

$ 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

这些命令也可以在此Makefile中查看,完整的代码库可以在此存储库中找到。

以下是来自Postgres容器的日志:

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.

我应该如何正确配置root角色?

英文:

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?

答案1

得分: 0

docker镜像文档指定如果未设置,POSTGRES_USER环境变量默认为postgres,尝试使用它而不是root,或者删除容器并使用正确的环境变量重新构建容器。

一旦你进入psql shell,你可以使用以下命令创建一个用户:

CREATE USER username WITH PASSWORD 'your_password';

然后,为用户授予对特定数据库的访问权限:

GRANT ALL PRIVILEGES ON DATABASE demodb TO username;

完成后,你可以在make文件的连接字符串中使用该用户。

英文:

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

答案2

得分: 0

原来,通过Hombrew在我的操作系统上安装和设置的Postgres服务器正在使用相同的端口,这与在相同端口号下对容器化数据库的请求发生冲突。

可以通过为容器化数据库使用不同的端口号或关闭操作系统上的数据库来解决此问题。

英文:

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.

huangapple
  • 本文由 发表于 2022年10月15日 18:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/74078604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定