Rails 使用 PostgreSQL

Yeng Tsan
4 min readJul 12, 2016

--

經過團隊一番討論,決定使用PostgreSQL,想使用的原因非常簡單,就是….想用啊 XD

就這樣踏入了這個坑

首先,先來個本機安裝PostgreSQL吧!

brew install postgresql

安裝完會看到

To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres

可以手動執行

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

安裝完了程式,首先就要有「資料庫」,否則開啟了PostgreSQL、Rails database.yml 設定好了,但PostgreSQL沒有資料庫的話,也是枉然

當然,安裝完之後需要啟動

brew services start postgresql
brew services stop postgresql
brew services restart postgresql

啟動不夠,還需要建立db才行

createdb db_name
psql db_name

createdb是PostgreSQL建立的指令,而psql則是進入db的指令,只要能進到db 的console模式,代表這個這個資料庫有建起來囉!

接下來就是Rails的時間啦!

gem install pg -- --with-pg-config=/usr/local/bin/pg_config

在gem file加上pg

gem 'pg'
bundle install

並在 config/database.yml 裡面做設定

development:
adapter: postgresql
encoding: unicode
database: db_name
pool: 5
username:
password:
test:
adapter: postgresql
encoding: unicode
database: db_name
pool: 5
username:
password:

接著把db建立起來,就大功告成了!

reference

https://launchschool.com/blog/how-to-install-postgresql-on-a-mac

http://blog.airweb.tw/2015/09/macpostgressql.html

Google postgresql 指令 mac

deploy會用到的用到的指令

sudo service postgresql status 查看伺服器db狀態
su - postgres 切換到 postgre user
psql my_db 進到該db

參考

https://www.linode.com/docs/databases/postgresql/

PostgreSQL指令

http://lolikitty.pixnet.net/blog/post/28167655-postgresql-%E6%8C%87%E4%BB%A4

http://lolikitty.pixnet.net/blog/post/28180749-postgresql%E6%8C%87%E4%BB%A4%E9%9B%86

--

--

Yeng Tsan
Yeng Tsan

Written by Yeng Tsan

Software developer, Career consultant, Product manager.開啟你的海外職涯,從日本開始 https://engineer.taiwan-career.com/

No responses yet