gpt4 book ai didi

postgresql - 无法连接到 PostgreSQL 数据库

转载 作者:行者123 更新时间:2023-11-29 13:19:15 25 4
gpt4 key购买 nike

我在连接数据库时遇到问题。我收到消息称角色“Darko”不存在。这是我的命令序列:

/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data -l logfile 
Server starting /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data The files belonging to this database system will be owned by user "darko".
This user must also own the server process.
The database cluster will be initialized with locales COLLATE: en_US.UTF-8 TYPE: en_US.UTF-8 MESSAGES: en_US.UTF-8 MONETARY: hr_HR.UTF-8 NUMERIC: hr_HR.UTF-8 TIME: en_US.UTF-8
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. initdb: directory "/usr/local/pgsql/data" exists but is not empty
If you want to create a new database system, either remove or empty the directory "/usr/local/pgsql/data" or run initdb with an argument other than "/usr/local/pgsql/data". /usr/local/pgsql/bin/psql psql:
FATAL: role "darko" does not exist

最佳答案

您正在尝试在“/usr/local/pgsql/data”目录中初始化 Postgres 集群(将存储所有 Postgres 数据和日志的主目录)。

在任何地方创建另一个目录,并将它的完整路径传递给 initdb 的 -D 选项。

它将初始化您的 Postgres 集群。

在此过程中,将创建默认数据库角色。如果您在常规操作系统用户(“darko”)下运行 initdb——它将创建具有相同名称的数据库用户。但通常人们在单独的操作系统用户“postgres”下运行 posgres 工具(包括 initdb)——如果你这样做(比如“sudo -u postgres initdb -D ...”),initdb 将创建数据库角色“postgres” “反而。 (顺便说一句,在 Postgres 术语中,数据库用户和数据库角色是一回事)。

然后,一旦 initbd 成功创建了您的 Postgres 集群,您可以使用“ls”检查它——目录必须包含子目录,例如“base”、“global”、“pg_xlog”、配置文件等。

然后你需要运行 Postgres,这是使用 pg_ctl 命令完成的,你需要将路径传递给你的集群,再次使用 -D 选项:

pg_ctl -D /path/to/cluster

如果运行成功,打开另一个终端选项卡/窗口并尝试使用 psql 和相应的数据库角色(“darko”或“postgres”,根据您之前的决定)进行连接:

psql -U darko template1

“template1”是一个始终存在的数据库名称,它用作您将创建的所有 future 数据库的模板。

此外,在您尝试使用 pg_ctl 启动 Postgres 之前,检查您是否已经运行了一些 Postgres 是值得的:

ps ax | grep postgres

-- 如果您有另一个 Postgres 正在运行,您可以使用“pg_ctl stop”将其关闭,并使用相应的 -D 选项指向正确的集群目录。

示例(Ubuntu):

$ ps ax | grep postgres | grep D
21996 ? S 23:37 /usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf

要优雅地停止它,只需运行:

/usr/lib/postgresql/9.6/bin/pg_ctl -D /var/lib/postgresql/9.6/main stop

关于postgresql - 无法连接到 PostgreSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44724437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com