ubuntu(12.04)にPostgreSqlをインストールして、外部の端末から接続できる所までのメモです。
※9.1も同様の手順(8.4→9.1に適宜変更する)でできました。
インストールするパッケージを探す
$ sudo aptitude search postgresql
いろいろ出てきた…この中から「postgresql-8.4」を入れてみます。
PostgreSql8.4インストール
$ sudo aptitude install postgresql-8.4
追加されたユーザー確認
$ cat /etc/passwd
~
postgres:x:105:114:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
ubuntuのパスワード変更
$ sudo passwd postgres
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
postgresユーザーにスイッチ
DBユーザー作成
$ createuser -P chapati
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) y
データベース作成
qsqlでいろいろ見てみる
データベース名
postgres=# select datname from pg_database;
datname
-----------
template1
template0
postgres
chapatidb
(4 rows)
テーブル名
postgres=# select tablename from pg_tables;
tablename
-------------------------
pg_type
省略
pg_auth_members
(49 rows)
ユーザー名 [#lb9e805e]
postgres=# select * from user;
current_user
--------------
postgres
(1 row)
chapatiが増えてない?
psql終了
外部から接続する設定
インストール直後のPostgreSqlは別のクライアントから接続出来ない設定になっています。
APサーバとDBサーバを一台で運用する分には問題ないのですが、
開発中は別々な方が便利なので、外部から接続出来るように設定します。
設定変更 postgresql.conf
$ vi /etc/postgresql/8.4/main/postgresql.conf
listen_addresses
デフォルトはローカルホストのみとなっているため、外部から接続できないので*にしてしまう。
#listen_addresses = 'localhost'
↓
listen_addresses = '*'
設定変更 pg_hba.conf
$ vi /etc/postgresql/8.4/main/pg_hba.conf
自分のIPアドレスを許可するように設定
host all all 自分のIPアドレス/32 md5
pgAdmin3で外部から接続しようとすると「000.000.000.000/24」を追加するといいよ!
って感じでエラーメッセージが表示されるので、
それを頼りに追加してもいいかもです。
PostgreSqlを再起動
$ sudo service postgresql restart
* Restarting PostgreSQL 8.4 database server [ OK ]
外部の端末からpgAdmin3で接続してみます
こんな設定で接続出来ましたっ
おまけ DBのpostgresユーザーのパスワード変更方法
psqlから、以下のSQLでパスワードを変更できます。
$ psql
psql (8.4.11)
Type "help" for help.
postgres=# alter role postgres with password '新しいパスワード';
ALTER ROLE
最終更新:2012年09月24日 01:49