Hatena::ブログ(Diary)

牌語備忘録 - pygo このページをアンテナに追加 RSSフィード

2012-01-25

postgresql91インストールメモ

*1

MacOSX10.7, psql (9.1.2)

インストール

sudo port install postgresql91
sudo port install postgresql91-server
postgresql91-serverインストールで表示されるメッセージのコマンド実行
sudo mkdir -p /opt/local/var/db/postgresql91/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql91/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb'

シンボリックリンク

確認

which postgres
  postgres not found
which pg_ctl
  pg_ctl not found

シンボリックリンクはる

sudo ln -s /opt/local/lib/postgresql91/bin/postgres /opt/local/bin
sudo ln -s /opt/local/lib/postgresql91/bin/pg_ctl /opt/local/bin

postgresql起動と停止

起動

sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql91/defaultdb start'

停止

sudo su postgres -c '/opt/local/bin/pg_ctl -D /opt/local/var/db/postgresql91/defaultdb stop'

TODO

コマンドにlogfile入れると『sh: logfile: Permission denied』になる。

そのうち何とかするかも。

sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql91/defaultdb -l /opt/local/var/db/postgresql91/logfile start'

設定など

前述のコマンドでpostgresqlを起動。

接続して、登録ユーザ確認。
psql -U postgres
psql (9.1.2)
Type "help" for help.

postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
パスワード設定

設定。

ALTER USER postgres WITH PASSWORD 'yourpassword';
ALTER ROLE
psqlを終了
postgres=# \q
pg_hba.conf

pg_hba.confを開いて、『METHOD』のところを書き換える。

sudo emacs /opt/local/var/db/postgresql91/defaultdb/pg_hba.conf

『trust』を 'yourpassword'に書き換え

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     yourpassword
# IPv4 local connections:
host    all             all             127.0.0.1/32            yourpassword
# IPv6 local connections:
host    all             all             ::1/128                 yourpassword

postgresql再起動

再起動して変更を反映

sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql91/defaultdb restart'

パスワード入力を確認

psql -U postgres
Password for user postgres: 
psql (9.1.2)
Type "help" for help.

postgres=#

Emacs -- sql-mode

  1. M-x sql-postgres
    1. User: postgres
    2. Databese:
    3. Server: localhost
    4. Password: password
  2. M-x sql-set-sqli-buffer




PythonとRubyでのクラスの継承とか書き方の違いのようなもの

Ruby

*2

Ruby その1

code

# -*- coding: utf-8 -*-

class Dog
  def initialize name, age
    @name = name
    @age = age
  end

  def name
    @name
  end

  def age
    @age
  end
end

class Breed < Dog
  def initialize name, age, breed
    super name, age
    @breed = breed
    @introduce = nil
  end

  def breed
    @breed
  end

  def introduce()
    @introduce = "#{@name} #{@age}歳、#{@breed}です。"
  end
end

taro = Dog.new("タロ", 6)
p taro
p taro.name
hachi = Breed.new("ハチ", 10, "秋田犬")
p hachi
p hachi.name
p hachi.age
p hachi.breed
p
p hachi.introduce()

execution result

#<Dog:0x007fcd5304c0e0 @name="タロ", @age=6>
"タロ"
#<Breed:0x007fcd5304bf50 @name="ハチ", @age=10, @breed="秋田犬", @introduce=nil>
"ハチ"
10
"秋田犬"
""
"ハチ 10歳、秋田犬です。"
Rubyその2

code

# -*- coding: utf-8 -*-

class Dog
  attr_accessor :name, :age

  def initialize name, age
    @name = name
    @age = age
  end
end

class Breed < Dog
  attr_accessor :name, :age, :breed

  def initialize name, age, breed
    super name, age
    @breed = breed
    @introduce = nil
  end

  def introduce()
    @introduce = "#{@name} #{@age}歳、#{@breed}です。"
  end
end

taro = Dog.new("タロ", 6)
p taro
p taro.name
hachi = Breed.new("ハチ", 10, "秋田犬")
p hachi
p hachi.name
p hachi.age
p hachi.breed
p ""
p hachi.introduce()

execution result

#<Dog:0x007fe4f184ca50 @name="タロ", @age=6>
"タロ"
#<Breed:0x007fe4f184c8c0 @name="ハチ", @age=10, @breed="秋田犬", @introduce=nil>
"ハチ"
10
"秋田犬"
""
"ハチ 10歳、秋田犬です。"

Python

code
#!/usr/bin/env python
# *-# -*- coding: utf-8 -*-


class Dog(object):

    def __init__(self, name, age):
        self.name = name
        self.age = age


class Breed(Dog):

    def __init__(self, name, age, breed):
        super(Breed, self).__init__(name, age)
        self.breed = breed

    def introduce(self):
        return "%(name)s %(age)s歳、%(breed)sです。" % \
            {"name": self.name, "age": self.age, "breed": self.breed}


taro = Dog("タロ", 6)
print taro
print taro.name

hachi = Breed("ハチ", 10, "秋田犬")
print hachi
print hachi.name
print hachi.age
print hachi.breed
print
print hachi.introduce()

execution result
<__main__.Dog object at 0x10fd1ad10>
タロ
<__main__.Breed object at 0x10fd1ad50>
ハチ
10
秋田犬

ハチ 10歳、秋田犬です。

参考サイト

メモ



*1:大幅改定: 20120126

*2:追加訂正:20120203


このページの先頭へ