labunix's blog

labunixのラボUnix

Debian Wheezyにpostgresql9.1を導入してみる。

2014/07/12、7.6がリリースされたようです。

 Debian「wheezy」リリース情報
 https://www.debian.org/releases/stable/
 
$ cat /etc/debian_version
7.6

■Debian Wheezyにpostgresql9.1を導入してみる。

$ sudo apt-get install -y postgresql postgresql-client

■起動確認

$ sudo /etc/init.d/postgresql status
Running clusters: 9.1/main 

$ sudo ps aux | grep post | sed s/".*[0-9]:[0-9]* "//g
/usr/lib/postfix/master
qmgr -l -t fifo -u
pickup -l -t fifo -u -c
/usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf
postgres: writer process                                                                                                    
postgres: wal writer process                                                                                                
postgres: autovacuum launcher process                                                                                       
postgres: stats collector process                                                                                           
grep post

■データベースの確認

$ sudo -u postgres psql -c "select now();"
              now              
-------------------------------
 2014-07-13 22:30:28.409821+09
(1)

$ sudo -u postgres psql -l 2>&1
                                         データベース一覧
   名前    |  所有者  | エンコーディング |  照合順序   | Ctype(変換演算子) |      アクセス権       
-----------+----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | 
 template0 | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
 template1 | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
(3)

$ sudo -u postgres psql -c "select oid,datname from pg_database;"
  oid  |  datname  
-------+-----------
     1 | template1
 11911 | template0
 11919 | postgres
(3)

■現在のユーザをスーパーユーザとして追加

$ sudo -u postgres createuser `whoami`
新しいロールをスーパーユーザにしますか? (y/n)y   

■パスワードの設定例
 ※joeパス、推測されやすいワード以外できちんと決めましょう。

$ sudo -u postgres psql -c "alter user `whoami` password '"`whoami`"'"
ALTER ROLE

$ PGPASS=password;sudo -u postgres psql -c "alter user `whoami` password '"$PGPASS"'";unset PGPASS
ALTER ROLE

$ psql -U `whoami` -d postgres -c "select current_user;"
 current_user 
--------------
 labunix
(1)

$ psql -U `whoami` -d postgres -c "select usename,passwd from pg_user;"
 usename  |  passwd  
----------+----------
 postgres | ********
 labunix  | ********
(2)


■ユーザ名と同名のDBを作成

$ createdb -U labunix labunix
$ psql -c "select oid,datname from pg_database;"
  oid  |  datname  
-------+-----------
     1 | template1
 11911 | template0
 11919 | postgres
 16385 | labunix
(4)

■「postgresql.conf」の設定
 デフォルト値

$ grep "^[a-z]" /etc/postgresql/9.1/main/postgresql.conf 
data_directory = '/var/lib/postgresql/9.1/main'		# use data in another directory
hba_file = '/etc/postgresql/9.1/main/pg_hba.conf'	# host-based authentication file
ident_file = '/etc/postgresql/9.1/main/pg_ident.conf'	# ident configuration file
external_pid_file = '/var/run/postgresql/9.1-main.pid'		# write an extra PID file
port = 5432				# (change requires restart)
max_connections = 100			# (change requires restart)
unix_socket_directory = '/var/run/postgresql'		# (change requires restart)
ssl = true				# (change requires restart)
shared_buffers = 32MB			# min 128kB
log_line_prefix = '%t '			# special values:
datestyle = 'iso, ymd'
lc_messages = 'ja_JP.UTF-8'			# locale for system error message
lc_monetary = 'ja_JP.UTF-8'			# locale for monetary formatting
lc_numeric = 'ja_JP.UTF-8'			# locale for number formatting
lc_time = 'ja_JP.UTF-8'				# locale for time formatting
default_text_search_config = 'pg_catalog.simple'

■IPを設定

$ netstat -an | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN     
tcp6       0      0 ::1:5432                :::*                    LISTEN     
unix  2      [ ACC ]     STREAM     LISTENING     436447   /var/run/postgresql/.s.PGSQL.5432

$ grep "listen" /etc/postgresql/9.1/main/postgresql.conf 
#listen_addresses = 'localhost'		# what IP address(es) to listen on;

$ ls -l /etc/postgresql/9.1/main/postgresql.conf
-rw-r--r-- 1 postgres postgres 19258  713 22:24 /etc/postgresql/9.1/main/postgresql.conf

$ sudo -u postgres vim /etc/postgresql/9.1/main/postgresql.conf
$ grep "listen" /etc/postgresql/9.1/main/postgresql.conf 
listen_addresses = '127.0.0.1'
#listen_addresses = 'localhost'		# what IP address(es) to listen on;

$ sudo -u postgres /etc/init.d/postgresql restart
[ ok ] Restarting PostgreSQL 9.1 database server: main.

$ netstat -an | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN     
unix  2      [ ACC ]     STREAM     LISTENING     449517   /var/run/postgresql/.s.PGSQL.5432

■クライアントに応じた接続許可のデフォルト値

$ sudo -u postgres grep -v "^#\|^\$" /etc/postgresql/9.1/main/pg_hba.conf 
local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

■UNIXソケット経由はpostgres以外、認証を必要とするように設定

$ sudo -u postgres vim /etc/postgresql/9.1/main/pg_hba.conf 
$ sudo -u postgres grep -v "^#\|^\$" /etc/postgresql/9.1/main/pg_hba.conf 
local   all             postgres                                peer
local   all             all                                     md5
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
$ sudo -u postgres /etc/init.d/postgresql reload
[ ok ] Reloading PostgreSQL 9.1 database server: main.

■ローカルのUNIXソケット経由でもパスワードを聞かれるようになる。

$ psql -c "select now();" 
パスワード: 
              now              
-------------------------------
 2014-07-13 23:22:54.127764+09
(1)

$ echo "select now();" | psql -U labunix -h 127.0.0.1 
ユーザ labunix のパスワード: 
             now              
------------------------------
 2014-07-13 23:19:59.44976+09
(1)

■postgresユーザだけはパスワードを聞かれない。
 それがいいかどうかは置いといて。。。

$ sudo -u postgres psql -U postgres -c "select now();" 
              now              
-------------------------------
 2014-07-13 23:23:45.526079+09
(1)