【问题标题】:set up postgres users, roles and databases for rails为 rails 设置 postgres 用户、角色和数据库
【发布时间】:2013-11-12 10:13:09
【问题描述】:

这真的让我卡住了,我有点想将 postgres 用于 rails 4

我已经安装了 postgres:

$ psql --version
psql (PostgreSQL) 9.1.10
contains support for command-line editing

$ sudo apt-get install libpq-dev 工作正常

我的 gemfile 中有 gem 'pg'

我的config/database.yml 看起来像这样:

development:
  adapter: postgresql
  encoding: unicode
  database: myafricastyle_development
  host: localhost
  pool: 5
  username: myafricastyle
  password: myafricastyle

创建数据库:$ sudo -u postgres createdb myafricastyle_development

这里是数据库:

$ psql
psql (9.1.10)
Type "help" for help.

jasonshark=# \l
                                           List of databases
           Name            |   Owner    | Encoding |   Collate   |    Ctype    |   Access privileges   
---------------------------+------------+----------+-------------+-------------+-----------------------
 jasonshark                | jasonshark | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 myafricastyle_development | postgres   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres                  | postgres   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0                 | postgres   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                           |            |          |             |             | postgres=CTc/postgres
 template1                 | postgres   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
                           |            |          |             |             | postgres=CTc/postgres
(5 rows)

但是当我尝试在 rails 项目目录中设置它时,我得到了这个:

$ rake db:create:all
FATAL:  password authentication failed for user "myafricastyle"
FATAL:  password authentication failed for user "myafricastyle"

编辑: 这是我的pg_hba.conf 文件:

local   all             postgres                                md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                md5
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

虽然我在编辑时做了sudo gedit pg_hba.conf,所以文件夹看起来有点时髦:

/etc/postgresql/9.1/main$ ls
environment  pg_hba.conf   pg_ident.conf    start.conf
pg_ctl.conf  pg_hba.conf~  postgresql.conf

我可以改变什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 postgresql-9.1 rails-postgresql pg


    【解决方案1】:

    你需要创建用户:

    sudo -u postgres createuser myafricastyle
    

    创建用户后,您还需要编辑 pg_hba.conf 文件,如下所述:

    http://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html

    【讨论】:

    • 好的,我使用该命令创建了一个用户(不是超级用户,允许创建数据库并且不允许创建新角色)但我得到相同的 password authentication failed 错误
    • 用户“myafricastyle”应该拥有数据库,而不是“postgres”。您必须为“myafricastyle”创建一个新角色,其中包括创建数据库的权利。然后,让“myafricastyle”创建表“myafricastyle_development”。
    • 澄清:sudo su - postgrescreate role myafricastyle with createdb login password 'myafricastyle'createdb -O myafricastyle myafricastyle_development
    猜你喜欢
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2016-03-30
    • 2012-03-08
    • 2017-10-07
    相关资源
    最近更新 更多