Ruby-on-rails 使用 Postgresql 数据库 (OSX Lion) 创建 Rails 3.1 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7741450/
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
Creating Rails 3.1 app with Postgresql database (OSX Lion)
提问by neon
I cant seem to find an up-to-date guide to creating a new Rails 3.1 app with a Postgresql database. I would greatly appreciate a guide through that process.
我似乎找不到使用 Postgresql 数据库创建新 Rails 3.1 应用程序的最新指南。我将不胜感激通过该过程的指导。
回答by bricker
Since Rails 3, the framework is completely database-agnostic.
从 Rails 3 开始,该框架完全与数据库无关。
What that means is, all you have to do is 2 things:
这意味着,您所要做的就是两件事:
- Include the
pggem in your Gemfile:gem 'pg' - Make sure your
database.ymlfile is usingpostgresqlas the adapter.
pg在你的 Gemfile 中包含gem:gem 'pg'- 确保您的
database.yml文件postgresql用作适配器。
You can accomplish this automatically when you create a new app by appending the --database=postgresqlflag:
您可以在创建新应用程序时通过附加--database=postgresql标志来自动完成此操作:
rails new myapp --database=postgresql
But if you've already created the app, then just comment out gem 'sqlite3'in your Gemfile, add in gem 'pg', run bundle, and then change your database.ymlfile to use the correct adapter.
但是,如果您已经创建了应用程序,那么只需gem 'sqlite3'在您的 Gemfile 中注释掉,添加 in gem 'pg', run bundle,然后更改您的database.yml文件以使用正确的适配器。
Of course, it goes without saying that you will also need to install PostgreSQL itself, but that is something you can easily find on google.
当然,不用说,您还需要安装 PostgreSQL 本身,但您可以在 google 上轻松找到。
回答by Lance Fisher
To elaborate on bricker's answer... After running:
详细说明砖家的答案......运行后:
$ rails new myapp --database=postgresql
Here is what your database.yml will look like:
这是您的 database.yml 的样子:
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password:
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password:
production:
adapter: postgresql
encoding: unicode
database: myapp_production
pool: 5
username: myapp
password:
Running:
跑步:
$ bundle exec rake db:create:all
will create the databases for you if you have PostgreSQL installed. The easiest way to install PostgreSQL is http://postgresapp.com/
如果您安装了 PostgreSQL,它将为您创建数据库。安装 PostgreSQL 的最简单方法是http://postgresapp.com/
回答by Keith
I found you have to either add the username, in the example above - myapp, to the PostgreSQL database or change the username to an existing user.
我发现您必须将上面示例中的用户名 - myapp 添加到 PostgreSQL 数据库或将用户名更改为现有用户。
回答by Starkers
As of Rails 4, bricker's answer does not work, at least not for me.
从 Rails 4 开始,砖家的回答不起作用,至少对我来说不是。
This command does the trick instead:
这个命令可以代替:
rails new new_app --d = postgres

