doureng1083 2015-09-23 03:57
浏览 49

golang —使用用户和密码连接到PostgreSQL时无法创建用户

I'm trying out the Chitchat go app from the book Go Web Programming. The original version works. When I use a user & password to access postgresql, it can connect to db but fails to create a new User, as shown below:

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
}

Here's the full code on Github.

However, I've found a temporary solution but it requires granting access to all the sequences in the chitchat database. Even after I've applied a

GRANT ALL PRIVILEGES ON DATABASE chitchat to tom;

it still requires manual grants to each of the table sequences. Here're the steps taken:

1) Grant access to database

GRANT all privileges on database chitchat to tom;

2) List all sequences in chitchat database

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

3) Grant access to each of them

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;

Is there a better way? Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dov11020 2015-09-23 04:16
    关注

    Only superuser can crete users in your postgress database. so make it so ..

    ALTER USER tom WITH SUPERUSER;

    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题