首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Ubuntu上使用Rails应用程序设置postgres数据库

在Ubuntu上使用Rails应用程序设置postgres数据库
EN

Stack Overflow用户
提问于 2013-11-06 10:19:44
回答 1查看 784关注 0票数 0

现在我对Ubuntu和postgres有点不放心,但这是我目前所做的。

在Ubuntu中,我创建了一个名为postgres的用户,密码为postgress:

代码语言:javascript
运行
复制
$ sudo adduser postgres
Enter new UNIX password: (I typed ' postgres ' here)
Retype new UNIX password: (I typed ' postgres ' here)

然后,我以postgres用户身份登录:

代码语言:javascript
运行
复制
$ su postgres
Password: (I typed ' postgres ' here)

作为postgres,我启动了postgres服务器:

代码语言:javascript
运行
复制
postgres@ubuntu:/$ /usr/lib/postgresql/9.1/bin/postgres -D /usr/local/var/postgres
LOG:  database system was shut down at 2013-11-05 17:04:32 PST
LOG:  database system is ready to accept connections 
LOG:  autovacuum launcher started

接下来,我创建了一个使用postgres数据库的rails应用程序设置:

代码语言:javascript
运行
复制
rails new myapp --database=postgresql

我附加了我的数据库配置YAML文件:

代码语言:javascript
运行
复制
# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
development:
  adapter: postgresql
  encoding: unicode
  database: postgresql_development
  pool: 5
  username: postgres
  password: postgres

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: postgresql
  encoding: unicode
  database: postgresql_test
  pool: 5
  username: postgres
  password: postgres

production:
  adapter: postgresql
  encoding: unicode
  database: postgresql_production
  pool: 5
  username: postgres
  password: postgres

最后,我运行

代码语言:javascript
运行
复制
rake db:setup

不知道会发生什么,真的。正如前面提到的,我对这个话题有点不太了解。我的应用程序的db目录中会出现许多小的db.postgres文件?

无论如何,我得到了一个巨大的错误消息,但它的要点是:

代码语言:javascript
运行
复制
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"postgresql_development", "pool"=>5, "username"=>"postgresql", "password"=>nil}
FATAL:  role "postgresql" does not exist

好消息是数据库服务器输出如下:

代码语言:javascript
运行
复制
FATAL:  role "postgresql" does not exist
FATAL:  role "postgresql" does not exist
FATAL:  role "postgresql" does not exist

这意味着在我的系统上一切都是正确的,并且rake任务正在尝试创建我的测试、生产和部署数据库。

坏消息是,我并不真正理解错误消息:

代码语言:javascript
运行
复制
FATAL:  role "postgresql" does not exist 

嗯,我很确定在我的YAML中指定的用户名是postgres,而不是postgresql。

代码语言:javascript
运行
复制
couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"postgresql_development", "pool"=>5, "username"=>"postgresql", "password"=>nil}

为什么我的用户名postgresql和密码都是空的?这根本不是我的YAML中的东西!

如果有人能帮我走出这最后一步,我将非常感激:)

最后一个问题,非sqlite3数据库到底是如何工作的,它们位于系统的什么位置。当我开始使用Rails并看到db目录中的小db.sqlite3时,我可以完全理解这一点。

然而,对于beefer数据库,数据库现在存储在称为集群的东西中,对吗?

现在,对于我创建的每个postgres rails应用程序,它们的所有数据库都会存储在集群中吗?

如果我购买一个Ubuntu VPS并在上面安装postgres,这会是相同的吗?位于/usr/local/var/postgres的群集包含我所有应用程序的所有数据库?

EN

回答 1

Stack Overflow用户

发布于 2013-11-10 10:49:44

你忽略了一些细节,比如你所在的操作系统和版本,你是如何安装PostgreSQL的,这使得这个问题更难回答。

你提到了Ubuntu,所以我猜你从Ubuntu到apt安装了PostgreSQL包。

如果是这样的话,PostgreSQL用户帐户已经存在,并且被配置为可以通过pg_hba.conf中的PostgreSQL套接字的对等身份验证进行访问。您可以通过以postgres unix用户身份运行命令来访问它,例如:

sudo -u postgres创建用户the_name

rake db:create:all

完成此操作并重新启动我的Rails服务器后,我回到了server_url,我很高兴地使用了Rails!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19802883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档