使用postgresql来作为rails的数据库
rails默认自带了个零配置的sqlite、这个做为开发还OK、不过不能并发写入的特性不适合作为生产使用、从头开始说吧P.S.:以下基于ubuntu server部署postgresql
=============安装postgresql并启动===============
首先在ubuntu下安装postgresql吧
sudo apt-get install postgresql
然后启动postgresql服务器
sudo /etc/init.d/postgresql-8.4 start
10.04下安装的postgresql默认是8.4,若是10.10,则不用加版本号
如果想停止将start改成stop就可以了
=============配置postgresql===============
postgresql默认只有一个用户、就是postgres、所以只能切换成这个用户登录
sudo su postgres -c psql template1
然后更改postgres的密码
ALTER USER postgres WITH PASSWORD 'newpassword';
再设置postgres的系统密码
sudo passwd postgres
然后创建用户数据库、postgresql会默认为每个用户自建一个用户数据库、
createdb
createdb是创建数据库的命令、后面可以加上数据库的名称、如果不加上任何参数、那么就是默认与当前用户名同名的数据库名称、
创建完数据库后就用psql daname来进入postgresql控制台来进行各种操作、这部分就不再复述了、可以参看相关资料:http://www.cnblogs.com/klobohyz/archive/2012/01/04/2311834.html
================配置rails项目==========================
首先当然是产生一个rails项目、或者迩已经存在rails项目现在想更改成postgresql都可以、首先修改项目文件中的Gemfile、然后添加这么一行
gem 'pg'
再执行一次bundle install
执行完毕后就安装好了postgresql的ruby组件了、现在开始配置一下rails项目中的config/database.yaml
development:
adapter: postgresql
encoding: unicode
database: herkurails_development
username: root
password: 123qwe
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: unicode
database: herkurails_test
username: root
password: 123qwe
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
database: herkurails_production
username: root
pool: 5
password: root
timeout: 5000
当然了、其中的password和username要更改成迩的
postgresql用户名、上面的database参数就是postgresql的数据库名称、必须先要去postgresql那里去新建这些数据库
先、不要指望rake会自动帮迩建立、使用配置文件中指定的用户依次使用createdb
dbname来创建上面的数据库吧、创建完毕后、就回到rails项目中、执行rake
db:migrate来更改rails对数据库的操作吧、注意执行rake命令的当前用户一定是要username中指定的用户、不然会执行失败、
P.S.:如果迩不想以默认的用户、就要使用createuser username来添加用户、然后为它添加密
ALTER USER username WITH PASSWORD 'new password'
然后用这个号码来登录ubuntu来执行以上操作、最好使用系统已经存在的帐户、如果不是就要自己新建同名的系统帐户
参考:http://www.cnblogs.com/klobohyz/archive/2012/01/04/2312342.html
页:
[1]