简体   繁体   English

在Ubuntu上为Ruby on Rails安装PostgreSQL

[英]Installing PostgreSQL on Ubuntu for Ruby on Rails

I currently have Ruby on Rails installed via RVM in Ubuntu 12.04. 我目前在Ubuntu 12.04中通过RVM安装了Ruby on Rails。 The default database is set up in SQLite3, but I'd like to switch to PostgreSQL for the purposes of pushing to Heroku. 默认数据库是在SQLite3中设置的,但是为了推送到Heroku,我想切换到PostgreSQL。 How can I accomplish this? 我怎么能做到这一点?

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.yml in app directory 修改app目录中的database.yml

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://mrfrosti.com/2011/11/postgresql-for-ruby-on-rails-on-ubuntu/
http://railscasts.com/episodes/342-migrating-to-postgresql http://railscasts.com/episodes/342-migrating-to-postgresql
https://devcenter.heroku.com/articles/local-postgresql https://devcenter.heroku.com/articles/local-postgresql

For all Ubuntu 13.10 users 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 . 因为Ubuntu 13.10没有正式的Postgres存储库。

Then create the user as Nick explain (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 gotqn above with whoami result: 注意:用whoami结果替换上面的gotqn

在此输入图像描述

The easiest way to create your rails application is to specify you are using postgresql as follows: 创建rails应用程序的最简单方法是指定使用postgresql ,如下所示:

rails new Demo -d postgresql

The code above will automatically add the pg gem in your GemFile and create appropriate database.yml file: 上面的代码会自动在你的GemFile添加pg gem并创建相应的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:create and start the rails server. 然后运行rake db:create并启动rails服务器。

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/ precise-pgdg main'> /etc/apt/sources.list.d/pgdg.list”

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

sudo apt-get update sudo apt-get update

sudo apt-get install postgresql-common sudo apt-get install postgresql-common

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM