Ruby-on-rails 如何为本地 Rails 项目设置 Postgres 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19953653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up Postgres database for local Rails project?
提问by Connor Leech
I recently got a new machine and would now like to work on my projects from Github. I'm curious as to how to properly set up the Postgres database on my local machine. I have postgresql, pgadmin3and libpq-devinstalled on Ubuntu (12.04).
我最近买了一台新机器,现在想在 Github 上处理我的项目。我很好奇如何在我的本地机器上正确设置 Postgres 数据库。我有postgresql,pgadmin3并libpq-dev安装在 Ubuntu (12.04) 上。
I pull down the project:
我拉下项目:
git clone https://github.com/thebenedict/cowsnhills.git
git clone https://github.com/thebenedict/cowsnhills.git
and run:
并运行:
bundle.
bundle.
When I run:
当我运行时:
rake db:create && rake db:schema:load
rake db:create && rake db:schema:load
I get this error:
我收到此错误:
rake db:create && rake db:schema:load
FATAL: password authentication failed for user "cnh"
FATAL: password authentication failed for user "cnh"
....
The config/database.ymlfile looks like this:
该config/database.yml文件如下所示:
development:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_development
pool: 5
username: cnh
password: cnh
test:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_test
pool: 5
username: cnh
password: cnh
production:
adapter: postgresql
encoding: unicode
host: localhost
database: cnh_production
pool: 5
username: cnh
password: cnh
What's the proper way to set up the Postgres database so I can run this project on my local machine?
设置 Postgres 数据库以便我可以在本地机器上运行这个项目的正确方法是什么?
Right now when I start the Rails server I get:
现在,当我启动 Rails 服务器时,我得到:


采纳答案by prasad.surase
sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update
#now install postgresql
sudo apt-get install postgresql-9.1 libpq-dev
在 psql 中创建一个新用户
sudo su postgres
createuser user_name #Shall the new role be a superuser? (y/n) y
文件
gem 'pg'
bundle install
捆绑安装
开发.ymldevelopment:
adapter: postgresql
database: app_development
pool: 5
username: user_name
password:
回答by Daniel
I came across your question when looking for the same answer. I attempted to follow the instructions @prasad.surase gave you. The problem I found is the ppa repository is going to depreciate soon on 12.04 LTS. Instead I found this link and it really helped.
我在寻找相同答案时遇到了您的问题。我试图按照@prasad.surase 给您的说明进行操作。我发现的问题是 ppa 存储库很快就会在 12.04 LTS 上贬值。相反,我找到了这个链接,它真的很有帮助。
PostgreSQL setup for Rails development in Ubuntu 12.04
用于 Ubuntu 12.04 中 Rails 开发的 PostgreSQL 设置
Install postgresql and admin tools through the package manager
sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3Login to postgresql prompt as the postgres user
sudo su postgres -c psqlCreate a postgresql user for your project
create user username with password 'password';Setup your postgres user with the same name and password as your Ubuntu user and make him a postgres superuser
alter user username superuser;Create the development and test databases
create database projectname_development; create database projectname_test;Give permissions to the user on the databases
grant all privileges on database projectname_development to username; grant all privileges on database projectname_test to username;
通过包管理器安装 postgresql 和管理工具
sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3以 postgres 用户身份登录到 postgresql 提示符
sudo su postgres -c psql为您的项目创建一个 postgresql 用户
create user username with password 'password';使用与您的 Ubuntu 用户相同的名称和密码设置您的 postgres 用户,并使他成为 postgres 超级用户
alter user username superuser;创建开发和测试数据库
create database projectname_development; create database projectname_test;授予用户对数据库的权限
grant all privileges on database projectname_development to username; grant all privileges on database projectname_test to username;
To end the postgresql session type \q
结束 postgresql 会话类型 \q
Update password for the user
更新用户密码
alter user username with password ‘new password';
回答by techvineet
You follow this link http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/
您可以点击此链接http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/
to create a postgres user and replace the credentials in database.yml
创建一个 postgres 用户并替换 database.yml 中的凭据

