介紹
Postgres是一個強大的免費關(guān)系型數(shù)據(jù)庫。自1995年問世以來悬荣,因為其出色的健壯性與強大的特性菠秒,Postgres獲得了良好的發(fā)展勢頭。本文我們將介紹在Mac OS X上安裝PostgreSQL的過程氯迂,并講解如何在Ruby on Rails項目中配置它践叠。
使用Homebrew安裝PostgreSQL
Homebrew是一個OS X系統(tǒng)中流行的包管理器。使用Homebrew安裝PostgreSQL的步驟如下:
如果之前沒有安裝過Homebrew的話嚼蚀,首先需要安裝它禁灼。
Homebrew站點提供了簡單的安裝命令,你只需要把它粘貼到終端執(zhí)行就好了轿曙。如果安裝過程中提示需要安裝其它命令行提示工具弄捕,一并將其安裝哮独。
下一步,執(zhí)行brew install postgres
安裝Postgres察藐∑よ担可能需要一段時間來完成編譯和安裝。安裝結(jié)束之后分飞,會給你一些提示來引導(dǎo)你完成設(shè)置悴务。
數(shù)據(jù)庫會在安裝的過程中進行初始化,使用Homebrew安裝PostgreSQL不用再執(zhí)行initdb
來完成安裝過程譬猫。如圖讯檐,在安裝快結(jié)束的時候,可以看到一些brew services
命令的提示染服。
如果還沒有安裝brew services别洪,可以使用以下命令進行安裝。
brew tap homebrew/services
然后使用以下命令以后臺服務(wù)的方式啟動Postgres
brew services start postgresql
執(zhí)行以上命令后柳刮,Postgres會在系統(tǒng)登錄之后自動重啟挖垛。
一旦Postgres啟動,我們也可以使用brew services來手工關(guān)閉它秉颗。
brew services stop postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
或者也可以使用brew services來重啟
brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
在這里我們完成了PostgreSQL的一切配置痢毒。
在Rails項目中配置PostgreS
首先安裝pg gem.
gem install pg -- --with-pg-config=/usr/local/bin/pg_config
或者將其添加到Gemfile中,執(zhí)行
bundle install
接下來蚕甥,設(shè)置config/database.yml使其指向Posgres數(shù)據(jù)庫哪替。
development:
adapter: postgresql
encoding: unicode
database: myapp_dev
pool: 5
username: your_username_on_mac
password:
test:
adapter: postgresql
encoding: unicode
database: myapp_dev
pool: 5
username: your_username_on_mac
password:
創(chuàng)建development和test數(shù)據(jù)庫
rake db:create:all
運行以下命令遷移未實行的遷移文件。
rake db:migrate