データベース(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'
    

パソコンでサーバーを構築して、自宅サーバにしよう。