【问题标题】:How to enable regular linux users to access postgres database without sudo access?如何让普通 linux 用户在没有 sudo 访问的情况下访问 postgres 数据库?
【发布时间】:2018-10-06 17:10:10
【问题描述】:

我们在 Centos7 上安装了一个 postgres 数据库服务器(10.3),并创建了一个名为“db_name”的数据库。我们有数据库访问权限。 pg_hba.conf 中的设置如下:

# "local" is for Unix domain socket connections only
local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident

当我有 sudo 访问权限时,我可以访问数据库“db_name”。

[root@localhost bin]# sudo -s       
[root@localhost bin]# psql -U db_name db_user
psql (10.3)
Type "help" for help.

db_name=>

当我尝试以普通 linux 用户身份访问数据库时,出现以下错误:

[linuxuser@localhost bin]# psql -U db_name db_user
psql: 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"?

我们想要限制某些用户 sudo 访问权限的原因是我们的报告专家需要访问数据库“db_name”,但我们不希望他们拥有 sudo 权限来执行其他操作。

我应该进行哪些设置才能使其正常工作? 谢谢!

【问题讨论】:

  • 尝试 TCP 连接:psql -h -U username dbname。另外:你没有提到你创建了一个用户,只有一个数据库。当您创建数据库时,Postgres 也不会自动为您创建用户。

标签: postgresql centos7 sudo


【解决方案1】:

在 PostgreSQL 中,您应该创建一个角色来访问数据库。

为此,您必须这样做:

  1. 更改为 postgres 帐户(在安装 postgresql 期间创建)

    $ sudo -i -u postgres
    
  2. 创建一个新角色

    postgres@server createuser --interactive
    
    Output
    Enter name of role to add: DB_Name
    Shall the new role be a superuser? (y/n) y
    
  3. 创建数据库

    postgres@server createdb DB_Name
    
  4. 创建用户,更改用户并访问数据库

    $ sudo adduser DB_Name
    $ sudo -i -u DB_Name
    $ psql
    DB_Name=# \conninfo
    

参考资料:

How To Install and Use PostgreSQL on CentOS 7

【讨论】:

    【解决方案2】:

    我找到了解决方案。 问题的原因是不存在 /var/run/postgresql/.s.PGSQL.5432 文件。 默认情况下,unix_socket_directories 是在 postgresql.conf 中设置的 '/tmp'。

    由于某种原因,普通 linux 用户不会查看 /tmp/.s.PGSQL.5432,而是查看 /var/run/postgresql/.s.PGSQL.5432 文件。

    所以修复如下:

    cd /var/run
    mkdir postgresql 
    cd postgresql/ 
    ln -s /tmp/.s.PGSQL.5432 .s.PGSQL.5432 
    

    以下命令同样有效。

    psql -h /tmp -U db_user db_name 
    

    【讨论】:

    • 你拯救了我的一天
    • 短版:sudo mkdir -p /var/run/postgresql && sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2019-10-18
    • 2012-08-21
    • 1970-01-01
    • 2017-04-29
    • 2023-01-12
    相关资源
    最近更新 更多