发生错误:连接到服务器时出错:致命:角色“postgres”不存在

时间:2016-09-03 10:06:10

标签: postgresql

我在Linux系统中使用entripeseDB安装了Postgres 9.5版本。

使用pgadmin3连接到Postgres并创建数据库和表。 然后我就能从laravel php应用程序中获取数据。

然后我的系统意外地发抖了。

重启后,我无法连接Postgres服务器。

错误是:

  

连接到服务器时出错:致命:角色“postgres”不存在。

这可能是什么原因?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

奇怪的是,如果没有你做任何删除超级用户的事情,就会发生这种情况。我将回答这个问题,好像你得到的错误是正确的,但我怀疑还有其他事情发生。

如果要相信错误消息,那么主要超级用户(postgres)不再存在于您的数据库中。根据你所说的,这是唯一的超级用户,所以要恢复它,你需要以单用户模式启动postgresql服务器。

我正在使用postgresql 9.5运行Ubuntu Server 16.04。您的系统上的目录可能有所不同

  1. 停止服务器

    sudo service postgres stop

  2. 登录拥有数据库的操作系统用户(对我而言,也称为postgres)。我用:

    sudo -u postgres bash
    
  3. 以单用户模式运行PostgreSQL服务器。您需要指定集群的配置目录和数据库名称(我的集群是9.5 / main,数据库也称为main)这应该为您提供后端提示:

    /usr/lib/postgresql/9.5/bin/postgres --single -D /etc/postgresql/9.5/main/
    
    PostgreSQL stand-alone backend 9.5.4
    backend>
    
  4. 在后端提示中添加一个名为postgres的新用户,其中包含superuser个权限:

    backend> create user postgres superuser;
    backend>
    
  5. 退出单用户模式。我按ctrl-d
  6. 以自己的身份重新登录。对我来说只是:

    exit
    
  7. 启动服务器:

    sudo service postgres start
    
  8. 查找postgresql运行的用户。确保服务器仍在运行(即,如果您已将其停止,则sudo service postgres start)。然后查找正在运行的进程:

    ps -ef | grep postgres
    

    对我来说,这给出了以下内容。请注意我的左栏是postgres。这是运行我的数据库集群的OS用户。

    postgres 25536     1  0 16:40 ?        00:00:01 /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf
    postgres 25539 25536  0 16:40 ?        00:00:00 postgres: checkpointer process
    postgres 25540 25536  0 16:40 ?        00:00:00 postgres: writer process
    postgres 25541 25536  0 16:40 ?        00:00:00 postgres: wal writer process
    postgres 25542 25536  0 16:40 ?        00:00:00 postgres: autovacuum launcher process
    postgres 25543 25536  0 16:40 ?        00:00:00 postgres: stats collector process
    couling   32550 23363  0 21:35 pts/0    00:00:00 grep --color=auto postgres
    

    有时此用户不会设置为登录,因此如果您的操作系统用户被调用postgres,请尝试使用:

    sudo -u postgres bash
    

    对于数据库名称,您需要群集中的任何有效数据库名称,因此如果您忘记了数据库名称,请尝试postgrestemplate0

相关问题