1.首先在ubuntu下安装postgresql
sudo apt-get install postgresql
2.启动postgresql服务器
sudo /etc/init.d/postgresql start
3.登陆postgresql
postgresql安装完成后默认只有一个用户、就是postgres、所以只能切换成这个用户登录
sudo su postgres -c psql
更改postgres的密码
ALTER USER postgres WITH PASSWORD '123456';
4.在rails中配置
1.在Gemfile中添加
gem 'pg'
再执行一次bundle install 安装gem
2.配置rails项目中的config/database.yml
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: 123456
# For details on connection pooling,see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
development:
<<: *default
database: cool_development
test:
<<: *default
database: cool_test
production:
<<: *default
database: cool_production
username: cool
password: <%= ENV['COOL_DATABASE_PASSWORD'] %>
3.解决ActiveRecord::NoDatabaseError (FATAL: database "cool_development" does not exist
rails不会自动给建立数据库 所以要手动建立自己要使用的数据库
createdb cool_development -U postgres
出现错误:psql: FATAL: Peer authentication Failed for user “postgres”,解决办法如下:
1. 运行下面的命令编辑pg_hba.conf文件
sudo gedit /etc/postgresql/9.1/main/pg_hba.conf
2. 将
`# Database administrative login by Unix domain socket
local all postgres peer`
改为
# Database administrative login by Unix domain socket
local all postgres trust
sudo /etc/init.d/postgresql reload
再执行 createdb cool_development -U postgres , 已经成功的创建了cool_development 数据库.