我试图在Cloud9的Rails应用程序中设置一个postgres数据库.
我按照这里的说明:https://docs.c9.io/setting_up_postgresql.html并设置了一个名为cc_database的数据库.
我的database.yml文件如下所示:
development: adapter: postgresql encoding: sql_ASCII database: cc_database pool: 5 username: postgres password: password
当我运行rake db:setup我得到以下错误:
PG::ConnectionBad: FATAL: Peer authentication Failed for user "postgres"
我对这一切很新,所以任何建议都将不胜感激.
解决方法
执行以下步骤:
>在cloud9上为postgresql创建一个新的用户名和密码:
$sudo service postgresql start $sudo sudo -u postgres psql postgres=# CREATE USER username SUPERUSER PASSWORD 'password'; postgres=# \q
>在cloud9上创建ENV变量:
$echo "export USERNAME=username" >> ~/.profile $echo "export PASSWORD=password" >> ~/.profile $source ~/.profile
我的database.yml for rails 4.2.0 on cloud9:
default: &default adapter: postgresql encoding: unicode pool: 5 username: <%= ENV['USERNAME'] %> password: <%= ENV['PASSWORD'] %> host: <%= ENV['IP'] %> development: <<: *default database: sample_app_development test: <<: *default database: sample_app_test production: <<: *default database: sample_app_production
在Gemfile中包含gem pg并安装:
gem ‘pg’,‘~> 0.18.2’
$bundle install
>更新cloud1上的database.yml的template1 postgresql:
postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1'; postgres=# DROP DATABASE template1; postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE'; postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1'; postgres=# \c template1 postgres=# VACUUM FREEZE; postgres=# \q
>从命令行运行:
bundle exec rake db:create