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

时间:2013-11-06 02:19:44

标签: ruby-on-rails ruby ubuntu rails-postgresql pg

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

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

$ sudo adduser postgres
Enter new UNIX password: (I typed ' postgres ' here)
Retype new UNIX password: (I typed ' postgres ' here)

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

$ su postgres
Password: (I typed ' postgres ' here)

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

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

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

rails new myapp --database=postgresql

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

# 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

最后,我跑

rake db:setup

真的不知道会发生什么。如上所述,我对这个话题有点不稳定。许多db.postgres文件会出现在我的应用程序的db目录中吗?

无论如何,我收到一条巨大的错误信息,但其中的一个要点是:

Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"postgresql_development", "pool"=>5, "username"=>"postgresql", "password"=>nil}
FATAL:  role "postgresql" does not exist

好消息是数据库服务器输出是这样的:

FATAL:  role "postgresql" does not exist
FATAL:  role "postgresql" does not exist
FATAL:  role "postgresql" does not exist

意味着所有内容都在我的系统上正确“连接”,并且rake任务正在尝试创建我的测试,生产和部署数据库。

坏消息是我真的不明白错误信息:

FATAL:  role "postgresql" does not exist 
嗯,非常确定我的YAML中指定的用户名是postgres,而不是postgresql。

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的集群包含我所有应用程序的所有数据库?

1 个答案:

答案 0 :(得分:2)

通过省略操作系统和版本,如何安装PostgreSQL等详细信息,您已经很难回答这个问题。

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

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

sudo -u postgres createuser the_name

rake db:create:all

执行此操作并重新启动我的Rails服务器之后,我又回到了server_url,我很高兴能够使用Rails!