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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了