PostgreSQLを使ったRails3の新規作成

環境

OS
Ubuntu10.10
Ruby
1.8.7(RVMでインストール)
Rails
3.0.7
DB
PostgreSQL8.4.8
Rails実行環境
Apache2+Passenger

手順

必要なgemをインストール
$ gem install pg
Fetching: pg-0.11.0.gem (100%)
Building native extensions.  This could take a while...
Successfully installed pg-0.11.0
1 gem installed
Railsアプリ作成
$ cd /var/www
$ rails new test -d postgresql
アプリ用PostgreSQLのユーザ作成

OSのpostgresユーザになって

$ su - postgres

ユーザ作成

$ createuser user1
新しいロールをスーパーユーザとしますか? (y/n) n
新しいロールにデータベース作成権限を与えますか? (y/n) y
新しいロールにロールを作成する権限を与えますか? (y/n) n

パスワード設定

$ psql template1
psql (8.4.8)
"help" でヘルプを表示します.

template1=# alter user user1 with password '1234';
ALTER ROLE
template1=# \q
RailsのDB設定ファイル($RAILS_ROOT/config/database.yml)を修正
$ cd /var/www/test1
  1 # PostgreSQL. Versions 7.4 and 8.x are supported.
  2 #
  3 # Install the pg driver:
  4 #   gem install pg
  5 # On Mac OS X with macports:
  6 #   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
  7 # On Windows:
  8 #   gem install pg
  9 #       Choose the win32 build.
 10 #       Install PostgreSQL and put its /bin directory on your path.
 11 development:
 12   adapter: postgresql
 13   encoding: utf8
 14   database: test_development
 15   pool: 5
 16   username: user1
 17   password: 1234
 18
 19   # Connect on a TCP socket. Omitted by default since the client uses a
 20   # domain socket that doesn't need configuration. Windows does not have
 21   # domain sockets, so uncomment these lines.
 22   #host: localhost
 23   #port: 5432
 24
 25   # Schema search path. The server defaults to $user,public
 26   #schema_search_path: myapp,sharedapp,public
 27
 28   # Minimum log levels, in increasing order:
 29   #   debug5, debug4, debug3, debug2, debug1,
 30   #   log, notice, warning, error, fatal, and panic
 31   # The server defaults to notice.
 32   #min_messages: warning
 33
 34 # Warning: The database defined as "test" will be erased and
 35 # re-generated from your development database when you run "rake".
 36 # Do not set this db to the same as development or production.
 37 test:
 38   adapter: postgresql
 39   encoding: utf8
 40   database: test_test
 41   pool: 5
 42   username: user1
 43   password: 1234
 44
 45 production:
 46   adapter: postgresql
 47   encoding: utf8
 48   database: test_production
 49   pool: 5
 50   username: user1
 51   password: 1234
/etc/postgresql/8.4/main/pg_hba.conf修正
$ sudo vim /etc/postgresql/8.4/main/pg_hba.conf
〜〜省略〜〜
 68 # DO NOT DISABLE!
 69 # If you change this first entry you will need to make sure that the
 70 # database
 71 # super user can access the database using some other method.
 72 # Noninteractive
 73 # access to all databases is required during automatic maintenance
 74 # (custom daily cronjobs, replication, and similar tasks).
 75 #
 76 # Database administrative login by UNIX sockets
 77 local   all         postgres                          ident
 78
 79 # TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
 80
 81 # "local" is for Unix domain socket connections only
 82 # local   all         all                               ident
 83 local   all         all                               password
 84 # IPv4 local connections:
 85 host    all         all         127.0.0.1/32          md5
 86 # IPv6 local connections:
 87 host    all         all         ::1/128               md5
確認

おしまい。