furutatsuのメモ帳

ぼくがソフトウェアを設定したことや組んだプログラムを見返すためのブログです。

Mac OS X 10.7でPostgreSQLを入れてみた&動かしてみた

前回の記事でRails(ホントはHeroku)のためにPostgreSQLを入れると書いたので、入れてみた。

インストール

$ brew install postgresql

インストールしたらこんなの出てきた。今後のインストールとか設定のために載せる。

# Build Notes

If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
  https://github.com/mxcl/homebrew/issues/issue/2510

To build plpython against a specific Python, set PYTHON prior to brewing:
  PYTHON=/usr/local/bin/python  brew install postgresql
See:
  http://www.postgresql.org/docs/9.1/static/install-procedure.html

# Create/Upgrade a Database

If this is your first install, create a database with:
  initdb /usr/local/var/postgres

To migrate existing data from a previous major version (pre-9.1) of PostgreSQL, see:
  http://www.postgresql.org/docs/9.1/static/upgrading.html

# Start/Stop PostgreSQL

If this is your first install, automatically load on login with:
  mkdir -p ~/Library/LaunchAgents
  cp /usr/local/Cellar/postgresql/9.1.2/org.postgresql.postgres.plist ~/Library/LaunchAgents/
  launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist

If this is an upgrade and you already have the org.postgresql.postgres.plist loaded:
  launchctl unload -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
  cp /usr/local/Cellar/postgresql/9.1.2/org.postgresql.postgres.plist ~/Library/LaunchAgents/
  launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist

Or start manually with:
  pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

And stop with:
  pg_ctl -D /usr/local/var/postgres stop -s -m fast

# Other

Some machines may require provisioning of shared memory:
  http://www.postgresql.org/docs/current/static/kernel-resources.html#SYSVIPC

If you want to install the postgres gem, including ARCHFLAGS is recommended:
    env ARCHFLAGS="-arch x86_64" gem install pg

To install gems without sudo, see the Homebrew wiki.

DBを作る

$ initdb /usr/local/var/postgres

DBを動かす

ログイン時に自動的に動かすのと手動で動かすのがあるけど、ここでは手動で行う方を示す。
Railで動かすためのテストなので自動は必要ないと思う。

DBを手動でフォアグラウンドで動かす
$ postgres -D /usr/local/var/postgres

止めるには^Cを入力する。

DBを手動でバッググラウンドで動かす
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
バッググラウンドで動かしているDBを手動で止める
$ pg_ctl -D /usr/local/var/postgres stop -s -m fast

RailsをHeroku上で動かしてみた

HerokuというWebサービスがあるので使ってみた。
Heroku | Cloud Application Platform http://www.heroku.com/

アカウント登録とクライアントの導入

とりあえずこれを見て、
Getting Started with Heroku | Heroku Dev Center http://devcenter.heroku.com/articles/quickstart
とりあえずアカウントを登録して、ツールをインストールした。

Herokuにログイン

$ heroku login

とりあえずRailsのアプリを作ってみよう

Getting Started with Rails 3.0 on Heroku/Cedar | Heroku Dev Center http://devcenter.heroku.com/articles/rails3

$ rails new hoge
$ cd hoge

それでもって、Gemfileを書き換える。
HerokuではPostgreSQLが使われているので、SQLiteからPostgreSQLにする。

gem 'sqlite3'
↓
gem 'pg'

そして、インストールする。

$ bundle install

Herokuに上げるぞー

RailsをHerokuに置くのにGitを使うので以下のコマンドを実行する。

$ git init
$ git add .
$ git commit -m "init"

そんで、Heroku上でアプリの置き場所を作る。
(xxxx, yyyy, 0000は任意の文字、数字が入る)

$ heroku create --stack cedar
Creating xxx-yyy-0000... done, stack is cedar
http://xxx-yyy-0000.herokuapp.com/ | git@heroku.com:xxx-yyy-0000.git
Git remote heroku added

そして、Herokuにあげる

$ git push heroku master

ブラウザで確認すると、
f:id:nyannyanboo:20120307155955p:image:w360
このように表示出来た。

とりあえずここまで。

Heroku上でデータベースをマイグレートしたり、ログを見ることなどができる。

Heroku上でのDBはPostgreSQLなので、開発(ローカル)でSQLite、リリース(Heroku)でPostgreSQLと、上げるたびにいちいちGemfileを書き換えなければならなくなりかったるい…

ローカルでのDBもPostgreSQLにしたいんだけど、このDBの導入も厄介だなぁ…入れるけどね

X11なしでGimpとInkscapeを動かそう

以前はMac OS X上で、GimpInkscapeを使うにはX11が必要だったが、それ無しで動かせるのでやってみた。

導入編

1.gtk2が入っているならば、これらとこれらの依存関係を消す

sudo port uninstall --follow-dependents pangomm cairomm pango cairo

2.そんでインストール

sudo port install gimp2 +no_x11 +quartz
sudo port install inkscape +no_x11 +quartz

動作確認編

この二つのソフトはどうにかん起動ができたんだけど、
これらの起動はターミナルからなので、QuickSilverやAlfredなどのランチャーを使っている方は少々不便かも(設定すれば話は別ですが)。
あと、レスポンスが遅いし、ショートカットキーが使えない時もある。
MacのgimpやinkscapeをX11なしで使いたい - notes plastiques

オブジェクト変数だけをsysoutすると

大学のTAやってて気づいたこと。
JavaでSystem.out.println(オブジェクト変数.toString)をSystem.out.println(オブジェクト変数)で出力すると、前者のメソッド呼び出しと同じ動作になるようです。

試しにこんなコードを作って、

class Hoge {
  String name;
  int    age;

  public Hoge(String name, int age) {
    this.name = name;
    this.age  = age;
  }

  public String toString() {
    return String.format("%s name:%s age:%d", super.toString(), name, age);
  }

  public static void main(String args[]) {
    Hoge foo = new Hoge("foo", 22);

    System.out.println("With    toString -> " + foo.toString());
    System.out.println("Without toString -> " + foo); // このままこのクラスのtoStringを呼び出すらしい
  }
}

実行してみたところ、

% java Hoge
With    toString -> Hoge@593d93f4 name:foo age:22
Without toString -> Hoge@593d93f4 name:foo age:22

toStringがあってもなくても、このメソッドが呼び出されることがわかった。

ちなみに、toStringを定義しない場合、ObjectクラスのtoString(super.toString)が呼び出されるらしい。

追記
オブジェクトの内容を表示するのに便利なtoStringメソッドですが、今のプログラムだと、クラスのメンバ変数が増えたりしたら減ったりしたらtoStringメソッドにもその変数を追加削除しなければならない。そんなのはめんどくさいので、リフレクションを使ったら便利だと思う。そうすることで、いちいちtoStringに変数の追加削除をしなくても済むから。

Mac OS XでWindows 7のスナップ機能を実現する方法

Windows 7にはスナップという機能がある.スナップは,例えば,ウインドウを画面左端にドラッグしたときに,そのウィンドウのサイズを半分にする機能である.

しかし,Mac OS X (Snow leopard)にはそのような機能がないので,探してみたところ"Shiftlt"というツールがあったので,それを落してインストールした.

Windowsのようにマウスでの操作はできないが,
キーボードを使っている僕にとってはとても便利なツールだ

情報源
Mac OSXでWindows Aero Snapを実現する「ShiftIt」 | HuIn.Log

Macで隠しファイルを表示させよう

Macで初期状態では隠しファイルが文字通り見えないので、
ターミナルを使って設定します。

そこで以下のようにコマンドを打って実行します。

% defaults write com.apple.finder AppleShowAllFiles -boolean true

Finderを再起動します。

% killall Finder

これで隠しファイルが表示されます。

元に戻すには以下のようにコマンドを実行します。

% defaults delete com.apple.finder AppleShowAllFiles
% killall Finder

Macの隠しファイルや隠しフォルダを表示する裏技 / Inforati http://inforati.jp/apple/mac-tips-techniques/system-hints/how-to-show-hidden-file-or-folder-of-macos.html