RailsでPostgresqlを使うときに毎回そのためのユーザーの作成方法を忘れてしまうのでメモ。
まずユーザーを作成
WindowsでChocolateyを使ってpostgresqlをインストールしてると初期パスワードはPostgres1234になってるので、
$ psql.exe -U postgres
ユーザ postgres のパスワード: ←ここでパスワードを入力
psql (9.2.1)
"help" でヘルプを表示します.
postgres=# create user deployer;
CREATE ROLE
postgres=# alter user deployer with superuser;
ALTER ROLE
これでパスワード無しのdeployerユーザーが必要な権限付きで作成される。
※ Linux系のOSならsudo -u postgres psqlでpsqlコマンドを使いましょう。
パスワード無しユーザーでもOKにする
これだけだとfe_sendauth: no password suppliedというエラーが出てしまうので
C:\PostgreSQL\data\pg_hba.confを開いて
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
を
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
に書き換える。
んで管理者権限でコンソールを開いて
net stop postgresql
net start postgresql
でPostgresqlのサービスを再起動させる。
残念ながらrestartは存在しない。なんでや。
Rails側の設定
あとはいつもどおりconfig/database.ymlをこんなかんじに設定します。
development:
  <<: *default
  database: project_development
  username: deployer
  password:
  host: localhost
  port: 5432
test:
  <<: *default
  database: project_test
  username: deployer
  password:
  host: localhost
  port: 5432</pre>
んで $ rake db:create, $ rake db:migrateでデータベースが作成されるのを確認したらOK!