涼風コンピュータblog

涼風 ・Rubyist, RubyやRuby on Railsに詳しくなっていきたいです

UbuntuにPostgreSQL 9.1をインストールする

UbuntuPostgreSQLをインストールする方法を説明します。

OSはUbuntu 12.04 LTS,データベースはPostgreSQLで動作確認しました。

PostgreSQLをインストールする。$はコマンドプロンプトです。$の次から入力してください。

$ apt-get install postgresql

PostgreSQLのバージョンを確認する。

$ psql --version
psql (PostgresSQL) 9.1.3

私が確認した環境ではPostgreSQLのバージョンは9.1.3でした。

PostgreSQLのスーパーユーザーであるpostgresユーザーに変更し、設定ファイルを編集します。

$ sudo su - postgres
$ cd /etc/postgresql/9.1/main/
$ vim ./pg_hba.conf

pg_hba.confファイルから以下の記述までカーソルを移動します。

# "local" is for Unix domain socket connections only
local all all peer

ローカルホストでどんなユーザーでもパスワード認証できるようにします。peerをmd5に変更します。

# "local" is for Unix domain socket connections only
#local all all peer
local all all md5

PostgreSQLを起動します。ただし、インストール後、自動的に起動しますので必要ありません。

/etc/init.d/postgresql start

PostgreSQLを再起動します。

/etc/init.d/postgresql restart

データベースを作成します。

createuser -a blog
createdb -E UTF8 -O blog blog_devlopment -T template0
psql -c "alter user blog with password 'blog'"

※パスワードは説明のために簡単なパスワードを使用していますが、ランダムな長い文字列が良いでしょう。 

blogユーザーでデータベースにログインします。

$ psql -U blog blog_devlopment
blog_deplopment=#

PostgreSQLを停止します。

/etc/init.d/postgresql stop