Rails on PostgreSQL


Outline
  1. 1. Install postgresql
  2. 2. Setup postgresql
  3. 3. Rails on PostgreSQL
  4. 4. Reference

又是隻大象
最近看到老師都在玩 PostgreSQl ,看看我之前都是用 MySQL 當做資料庫的,感覺應該也來跟進下,雖然不前太知道他有什麼優缺點,但是學長聽說 MySQL 之後會閉源了,沒有人維護的確是很大的問題,總之就先來玩玩看吧。

Install postgresql

1
2
3
4
5
6
7
$ brew update
$ brew doctor
$ brew install postgresql
$ initdb /usr/local/var/postgres -E utf8
$ gem install lunchy
$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/postgresql/9.2.1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
  • Ubuntu
1
$ sudo apt-get install postgresql
  • check & start
1
2
3
4
5
6
7
$ postgres -V
$ lunchy start postgres
# for ubuntu
$ sudo /etc/init.d/postgresql start
$ brew info postgres

Setup postgresql

1
2
3
4
5
$ lunchy start postgres
$ lunchy stop postgres
# for ubuntu
$ sudo /etc/init.d/postgresql start
  • add user
1
2
3
4
$ sudo su - postgres # for ubuntu error
$ createuser --createdb --pwprompt username
$ dropuser root

Rails on PostgreSQL

  • Gemfile
1
gem 'pg'
1
2
3
$ sudo aptitude install libpq-dev # error for ubuntu
$ bundel install
  • config/database.yml
1
2
3
4
5
6
7
8
9
10
development:
adapter: postgresql
encoding: utf8
database: ass_development
pool: 5
username: root
password: 1234
host: localhost
...
  • Rake
1
2
3
$ rake db:create:all
$ rake db:migration
$ rake db:seed
  • for production
1
2
3
4
$ rake db:drop RAILS_ENV=production
$ rake db:create RAILS_ENV=production
$ rake db:migrate RAILS_ENV=production
$ rake db:seed RAILS_ENV=production

Reference