How do I solve this problem to use psql? | psql: error: FATAL: role "postgres" does not exist

How do I solve this problem to use psql? | psql: error: FATAL: role "postgres" does not exist

我在使用 PostgreSQL 时遇到问题。我最近在 ubuntu 20.04.

安装了这个版本 (13+223.pgdg20.04+1) 的 postgresql 包

我正在尝试 运行 psql 命令,但出现以下错误:

psql: error: FATAL: role "my_username" does not exist

我尝试使用 createuser me 创建新用户,但出现以下错误:

createuser: error: could not connect to database template1: FATAL: role "my_username" does not exist

我也尝试用 createuser me --username=postgres 强制 postgres 用户,但我收到以下错误:

createuser: error: could not connect to database template1: FATAL: Peer authentication failed for user "postgres"

如何解决这些问题以在我的计算机上本地使用 PostgreSQL 而不会出现这些问题?

PD:我已经重新安装了 postgres,现在我在执行 psql:

时遇到了不同的错误

psql: error: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

您需要在 psql 命令中使用 -U 选项提供用户名。

psql -U postgres

Postgresql 带有一个名为 postgres 的预定义超级用户角色。如果你想创建更多的角色,你首先必须连接这个初始角色。

对等身份验证意味着(有高级可能性,但默认情况下不会使用这些方法,而简单方法是 apt 安装的 PostgreSQL 的默认方法)你必须是 OS 用户'postgres' 以数据库用户 'postgres' 的身份连接。所以你会这样做:

sudo -u postgres createuser me

您无需指定 --username=postgres,因为一旦您使用 sudo -u postgres

,这就是默认行为

或者,如果您愿意,您可以更改 pg_hba.conf 以使用不同于对等的其他身份验证方法。

我不确定为什么我的安装有问题,但我已经完全卸载了 postgres post:

https://kb.objectrocket.com/postgresql/how-to-completely-uninstall-postgresql-757

之后,我重新启动了计算机并按照以下中的正确说明再次安装了 posgres:

https://www.postgresql.org/download/linux/ubuntu/

现在看起来没问题了