在 Ubuntu 上为 Ruby on Rails 安装 PostgreSQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11092807/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:34:14  来源:igfitidea点击:

Installing PostgreSQL on Ubuntu for Ruby on Rails

ruby-on-railsrubypostgresqlubuntuheroku

提问by Nick

I currently have Ruby on Rails installed via RVM in Ubuntu 12.04. The default database is set up in SQLite3, but I'd like to switch to PostgreSQL for the purposes of pushing to Heroku. How can I accomplish this?

我目前在 Ubuntu 12.04 中通过 RVM 安装了 Ruby on Rails。默认数据库是在 SQLite3 中设置的,但为了推送到 Heroku,我想切换到 PostgreSQL。我怎样才能做到这一点?

回答by Nick

Here are the steps I've followed:

以下是我遵循的步骤:

Install PostgreSQL and development package

安装 PostgreSQL 和开发包

$ sudo apt-get install postgresql
$ sudo apt-get install libpq-dev

Set up a user that is the same as my Ubuntu log-in

设置一个和我的Ubuntu登录一样的用户

$ sudo su postgres -c psql
postgres=# CREATE ROLE <username> SUPERUSER LOGIN;
postgres=# \q

Modify Gemfile

修改 Gemfile

# Remove gem 'sqlite3'
gem 'pg'

Modify database.ymlin app directory

database.yml在app目录修改

development:
  adapter: postgresql
  encoding: unicode
  database: appname_development
  pool: 5
  timeout: 5000
  username: <username>
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: appname_test
  pool: 5
  timeout: 5000
  username: <username>
  password:

Run bundle install

运行捆绑安装

$ bundle install

Create databases and migrations

创建数据库和迁移

$ rake db:create:all
$ rake db:migrate

Here are the sources I used to help:
http://mrfrosti.com/2011/11/postgresql-for-ruby-on-rails-on-ubuntu/
http://railscasts.com/episodes/342-migrating-to-postgresql
https://devcenter.heroku.com/articles/local-postgresql

以下是我用来帮助的来源:http:
//mrfrosti.com/2011/11/postgresql-for-ruby-on-rails-on-ubuntu/
http://railscasts.com/episodes/342-migrating-to -postgresql
https://devcenter.heroku.com/articles/local-postgresql

回答by gotqn

For all Ubuntu 13.10users that open this thread follow the steps below to install postresql:

对于Ubuntu 13.10打开此线程的所有用户,请按照以下步骤进行安装postresql

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common -t saucy
sudo apt-get install postgresql-9.2 libpq-dev

since there isn't an official Postgres repository for Ubuntu 13.10.

因为没有官方的 Postgres 存储库Ubuntu 13.10

Then create the user as Nickexplain (you can specify a password too):

然后创建用户作为Nick解释(您也可以指定密码):

sudo su postgres -c psql
postgres=# CREATE ROLE gotqn SUPERUSER LOGIN;
postgres=# \password gotqn
postgres=# \q

Note: Replace the gotqnabove with whoamiresult:

注意:将gotqn上面的替换为whoami结果:

enter image description here

在此处输入图片说明

The easiest way to create your rails application is to specify you are using postgresqlas follows:

创建 rails 应用程序的最简单方法是指定您正在使用postgresql如下:

rails new Demo -d postgresql

The code above will automatically add the pggem in your GemFileand create appropriate database.ymlfile:

上面的代码会自动添加pggemGemFile并创建合适的database.yml文件:

development:
  adapter: postgresql
  encoding: unicode
  database: Demo_development
  pool: 5
  username: gotqn
  password: mypass

Note: You need to change the username and to specify the correct password if you have set such.

注意:如果您已设置,您需要更改用户名并指定正确的密码。

Then run rake db:createand start the rails server.

然后运行rake db:create并启动 rails 服务器。

回答by Anjan

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"

sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/precision-pgdgmain' > /etc/apt/sources.list.d/pgdg.list"

wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc| sudo apt-key add -

wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc| sudo apt-key 添加 -

sudo apt-get update

sudo apt-get 更新

sudo apt-get install postgresql-common

sudo apt-get install postgresql-common

sudo apt-get install postgresql-9.3 libpq-dev

须藤 apt-get 安装 postgresql-9.3 libpq-dev