rails 3 postgreSQL 基本'数据库不存在'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8492038/
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
rails 3 postgreSQL basic 'database does not exist'
提问by sam452
OK, I'm building my first rails 3.0 app and want to test the postgreSQL server as production on my development machine (which is running 10.6). When you create a new app and rake db:migrate it creates sqlite db's for all three environments. Cool. Now I want to learn how to move to production and use postgres. I've used homebrew to install postgres, installed the pg (env ARCHFLAGS="-arch x86_64" gem install pg)
and postgres-pr gems
.
好的,我正在构建我的第一个 rails 3.0 应用程序,并希望在我的开发机器(运行 10.6)上测试作为生产环境的 postgreSQL 服务器。当您创建一个新应用程序并 rake db:migrate 时,它会为所有三种环境创建 sqlite db。凉爽的。现在我想学习如何转向生产并使用 postgres。我使用自制软件安装了 postgres,安装了 pg(env ARCHFLAGS="-arch x86_64" gem install pg)
和postgres-pr gems
.
I've run rake db:migrate
in hope that like with sqlite3 it will auto build my production server since I've updated my database.yml (see below).
我已经运行了 rake db:migrate
,希望像 sqlite3 一样,它会自动构建我的生产服务器,因为我已经更新了我的 database.yml(见下文)。
OK, in my app's folder, I restart the server using 'rails s --environment=production
' and it bails saying it cannot find my production database.
好的,在我的应用程序文件夹中,我使用“ rails s --environment=production
”重新启动服务器,但它说找不到我的生产数据库。
So all the google searches for 'rails 3 postgres install' got me this far, but I appear to be missing something because rails is failing to create the new pg database.
因此,所有对“rails 3 postgres install”的谷歌搜索都让我走到了这一步,但我似乎遗漏了一些东西,因为 rails 无法创建新的 pg 数据库。
postgres is running as determined by ps.
postgres 正在运行,由 ps 确定。
createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production
But this directory does not have this database so I'm missing something. What am I overlooking?
但是这个目录没有这个数据库,所以我错过了一些东西。我在看什么?
this is my database.yml snippet:
这是我的 database.yml 片段:
production:
adapter: postgresql
host: localhost
database: db/vitae_production
pool: 5
timeout: 5000
username: mysuperusername
password:
回答by mu is too short
There are a couple things going on here. First of all, you seem to be mixing the SQLite and PostgreSQL format for the database:
setting in your database.yml
. With SQLite, you specify the relative path to the SQLite database filewith something like:
这里有几件事情正在发生。首先,你似乎是混合的SQLite的和PostgreSQL格式database:
设置你的database.yml
。使用 SQLite,您可以使用以下内容指定 SQLite 数据库文件的相对路径:
database: db/vitae_production.sqlite
but with PostgreSQL, you specify the database namewith something like this:
但是对于 PostgreSQL,您可以使用以下内容指定数据库名称:
development:
database: vitae_development
username: rails
...
Then, once database.yml
is setup, you'd create the database user(if needed) from inside psql
:
然后,一旦database.yml
设置完成,您将从内部创建数据库用户(如果需要)psql
:
psql> create role rails login;
and then let Rails create the database:
$ rake db:create
Then you should be able to run your migrations to create your initial tables and away you go.
然后你应该能够运行你的迁移来创建你的初始表,然后你就可以走了。
You probably want to read the create role
documentation to make sure you get the right options. You probably want to be working in the development environment rather than production as well so I changed the names and YAML to reflect that, production is for deployment and I don't think you're deploying anything just yet.
您可能想阅读create role
文档以确保获得正确的选项。您可能还想在开发环境中工作而不是在生产环境中工作,因此我更改了名称和 YAML 以反映这一点,生产用于部署,我认为您还没有部署任何东西。
回答by Francisco Valdez
Im'not familiar with rails but you could create your database as a postgres super user and then grant privileges, assuming that in your linux distribution the postgres super user is postgres:
我不熟悉 rails,但您可以以 postgres 超级用户的身份创建数据库,然后授予权限,假设在您的 linux 发行版中,postgres 超级用户是 postgres:
su root
su postgres
createdb vitae_production
then in postgres grant privileges to another user
然后在 postgres 中授予另一个用户权限
psql
CREATE USER rails WITH PASSWORD 'myPassword';
GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
ALTER DATABASE vitae_production OWNER TO rails;
then your config file should look like this:
那么你的配置文件应该是这样的:
production:
adapter: postgresql
host: localhost
database: vitae_production
pool: 5
timeout: 5000
username: rails
password: myPassword