データベース(PostgreSQL)の構築

データベース(PostgreSQL)の構築

2004/1/19 :FreeBSDのカーネル及びユーザーランドを再構築したら、PostgreSQL 7.4.1でデータベースの初期化に成功した。 2004/5/29 :データベースのバック方法追加

はじめに

前のサーバでbookmark+xというブックマークソフトを使用していた。こちらでも使えるようにPostgreSQLをインストールする。

コンパイル&インストール

PostgreSQL 7.4.1のインストール

FreeBSDのportsシステムを使用してPostgreSQL 7.4.1をインストールした。

 # cd /usr/ports/database/postgresql7
 # make
 # make install
 # make clean

PostgreSQLの設定

データベースの初期化

 # su -l pgsql -c initdb
==[2004/1/12]PostgreSQL 7.4.1ではデータベースの初期化でエラーが出ます。==

設定ファイルの編集

 # cd /usr/local/pgsql/data
 # vi postgresql.conf
  • PostgreSQLサーバーにインターネット経由でアクセスできるように設定する。

    #tcpip_socket = false
     を
    tcpip_socket = true
     する
    

データベースの起動

 # /usr/local/etc/rc.d/010.pgsql.sh start

ユーザーの追加

  • 自分とapacheから使えるようにwwwを追加

    # su -l pgsql -c "createuser user1"
    Shall the new user be allowed to create databases? (y/n) y
    Shall the new user be allowed to create more new users? (y/n) y
    CREATE USER
    # su -l pgsql -c "createuser www"
    Shall the new user be allowed to create databases? (y/n) y
    Shall the new user be allowed to create more new users? (y/n) n
    CREATE USER
    

テスト

テスト用データベース作成

$ createdb testdb
CREATE DATABASE

psqlでデータベーステスト

$ psql testdb
Welcome to psql 7.4.1, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help on internal slash commands
       \g or terminate with semicolon to execute query
       \q to quit

testdb=# \l
       List of databases
   Name    | Owner | Encoding
-----------+-------+-----------
 template0 | pgsql | SQL_ASCII
 template1 | pgsql | SQL_ASCII
 testdb    | user1 | SQL_ASCII
(3 rows)

testdb=#\q
$

bookmark+xのインストール

bookmark+xのインストール

データベスのバックアップ

データベースbookmarkx、mt_dbをバックアップ、リストアする。

データベースのバックアップ(個別)

  • 旧データベースにアクセスする

     # su pgsql -c '/usr/local/bin/pg_ctl -D /mnt/usr/local/pgsql/data start'
    
  • データのバックアップ

     # su pgsql -c 'pg_dump bookmarkx > dbbackup-bookmarkx'
     # su pgsql -c 'pg_dump mt_db > dbbackup-mt_db'
    

データベースのリストア(個別)

  • 新データベースにアクセスする

     # su pgsql -c '/usr/local/bin/pg_ctl -D /mnt/usr/local/pgsql/data stop'
     # /usr/local/etc/rc.d/010.pgsql.sh start
    
  • データのリストア

     # su pgsql -c 'createdb -E EUC-JP bookmarkx'
     # su pgsql -c 'createdb mt_db'
     # su pgsql -c 'psql -e bookmarkx < dbbackup-bookmarkx'
     # su pgsql -c 'psql -e mt_db < dbbackup-mt_db'