Cloud9 postgres
我正在尝试在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
我在cloud9上的rails 4.2.0的database.yml:
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
-
在cloud9上更新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
cloud9中的postgresql设置为在localhost连接时与对等方进行身份validation。 因此,快速解决方案是将database.yaml中的用户更改为当前用户。 在我的例子中,用户名是ubuntu。 要查看当前用户,请使用该命令
$ echo $USER
所以终端中的命令列表是。
$ sudo su - postgres $ createuser ubuntu -dslP $ Enter password for new role: **same password from your yaml file** $ Enter it again:
像这样设置你的yaml文件
development: adapter: postgresql encoding: SQL_ASCII database: cc_database pool: 5 username: ubuntu password: password
现在你可以跑了
rake db:create rake db:migrate
使用带有空白密码的用户名“ubuntu”。 我的database.yml看起来像这样:
发展:
适配器:postgresql
编码:unicode
数据库:myflix_development
游泳池:5
用户名:ubuntu
密码:
测试:
适配器:postgresql
编码:unicode
数据库:myflix_test
游泳池:5
用户名:ubuntu
密码:
如果它然后抱怨密码错误,请尝试使用Cloud9的说明更改密码:
在命令行中,键入: sudo sudo -u postgres psql
postgres =# \password
输入新密码: leave it blank and press enter
再次输入: leave it blank and press enter
postgres =# \q
我对此很陌生。 希望有效!
如何在Cloud9上设置PostgreSQL和Rails
在撰写本文时,Cloud9预装了PostgreSQL,因此您无需自行安装。 但是,它默认情况下不运行,因此您需要在终端中使用此命令启动它:
sudo service postgresql start
将PostgreSQL密码更改为“密码”(或选择其他密码):
sudo sudo -u postgres psql # This will open the psql client. # Type \password and press enter to begin process # of changing the password: postgres=# \password # Type your new password (eg "password") and press enter twice: Enter new password: Enter it again: # Password changed, quit psql with \q postgres=# \q
将config/database.yml
编辑为:
default: &default adapter: postgresql encoding: unicode pool: 5 # Important configs for cloud9, change password value # to what you entered in the previous psql step. template: template0 username: ubuntu password: password development: <<: *default database: your_app_name_development test: <<: *default database: your_app_name_test production: <<: *default database: your_app_name_production username: your_app_name password: <%= ENV['YOUR_APP_NAME_DATABASE_PASSWORD'] %>
(注意上面default
部分中的template
, username
和password
配置是必不可少的)。
将pg
gem添加到Gemfile
:
gem 'pg'
运行bundle install
。
从Gemfile
删除sqlite gem(并可选择删除db/*.sqlite3
Gemfile
文件)。
使用db:setup
任务创建数据库,加载schema.rb并为数据库db:setup
种子:
bundle exec rake db:setup # Run bin/rake -AD db to see all db-related tasks
启动或重新启动rails应用程序并检查它是否正常工作。
请注意,旧数据库中的非种子数据不会出现在新数据库中。
如果您想使用psql客户端直接与PostgreSQL交互,请在终端运行psql
或运行bin/rails db
。
对我来说, 使用Rails和Postgresql执行Cloud9工作区设置中的步骤是不够的。 它传递给我的用户,但不是密码。 echo $USERNAME
一无所获。
解
$ sudo su - postgres $ createuser ubuntu -dslP
然后我这样做了:
sudo sudo -u postgres psql postgres=# \password Enter new password: entered a real password Enter it again: entered it again postgres=# \q
然后我在我的yaml文件中做了这个(注意我杀了主机部分):
development: adapter: postgresql encoding: unicode database: my_database_name pool: 5 username: ubuntu password: actual_password
然后我能够创建我的数据库:
rake db:create
我的Rails服务器开始没有任何打嗝。
找到了解决方案。 需要编辑pg_hba.conf文件以将身份validation从对等方更改为md5,如下所示:
local postgres postgres md5
很难找到该文件,因为它只能通过cloud9中的终端访问。 您无法在文件树中找到它。
如果您在postgres中键入以下内容,它将显示位置
SHOW hba_file;
然后,您可以通过终端在vim中查找和编辑。
在创建任何数据库之前使用rails应用程序时的简短版本:
添加gem’pg’删除gem sqlite3
$ bundle install $ gem install pg
然后,
$sudo service postgresql start $psql -c "create database myapp_development owner=ubuntu"
根据https://community.c9.io/t/how-do-i-set-up-postgresql-on-c9-for-my-rails-app/2614/4
改变myapp的名字。 然后,在下面复制粘贴,更改myapp的任何名称。
/myapp/config/database.yml development: adapter: postgresql encoding: unicode database: myapp_development pool: 5 username: ubuntu password: timeout: 5000
如上所述,无需密码,用户名可以保留ubuntu。
$rake db:migrate
如果您使用的是heroku,则不需要database.yml中的生产部分