【问题标题】:golang -- Cannot create user when using user & password to connect to postgresqlgolang - 使用用户和密码连接到 postgresql 时无法创建用户
【发布时间】:2015-12-20 05:56:33
【问题描述】:

我正在试用 Go Web Programming 一书中的 Chitchat go 应用程序。原始版本有效。当我使用用户密码访问postgresql时,它可以连接到db但无法创建新用户,如下所示:

func db() (database *sql.DB) {
    database, err := sql.Open("postgres", "dbname=chitchat user=tom password=tomahawk sslmode=disable")
    if err != nil {
        log.Fatal(err)
        fmt.Println("Db connection failed")
    }
    return
}

这是Github上的完整代码。

但是,我找到了一个临时解决方案,但它需要授予对 chitchat 数据库中所有序列的访问权限。即使在我申请了

GRANT ALL PRIVILEGES ON DATABASE chitchat to tom;

它仍然需要手动授予每个表序列。以下是采取的步骤:

1) 授予对数据库的访问权限

GRANT all privileges on database chitchat to tom;

2) 列出 chitchat 数据库中的所有序列

SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';

3) 授予他们每个人的访问权限

GRANT all privileges on sequence users_id_seq to tom;
GRANT all privileges on sequence threads_id_seq to tom;
GRANT all privileges on sequence posts_id_seq to tom;
GRANT all privileges on sequence sessions_id_seq to tom;

有没有更好的方法?提前致谢。

【问题讨论】:

    标签: postgresql go


    【解决方案1】:

    只有超级用户才能在你的 postgress 数据库中创建用户。所以就这样吧..

    ALTER USER tom WITH SUPERUSER;

    【讨论】:

    • 这个问题在这方面措辞不佳,但我很确定他们正在谈论创建应用程序用户(例如a plain insert into a users table)并且他们没有尝试创建新的PostgreSQL用户/角色。就像 Unix 中的 uid 0/root 一样,让应用程序使用具有完全且完全访问数据库的用户/角色进行连接是一个坏主意。
    猜你喜欢
    • 1970-01-01
    • 2013-06-30
    • 2021-01-21
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    相关资源
    最近更新 更多