CakePHPからPostgreSQLのpsqlコマンドを実行する方法
やんごとなき事情でCakePHPからPostgreSQLのpsqlコマンドを実行する場合は以下のようにします。
App::uses('ConnectionManager', 'Model');
// DBの設定情報を取得
$db = ConnectionManager::getDataSource('default');
// 実行するコマンドラインを構築
$cmd = '';
$cmd .= ' env PGPASSWORD=' . $db->config['password'];
$cmd .= ' psql';
$cmd .= ' -U ' . $db->config['login'];
$cmd .= ' -h ' . $db->config['host'];
$cmd .= ' -p ' . $db->config['port'];
$cmd .= ' -d ' . $db->config['database'];
$cmd .= ' -c "SQL文"';
exec($cmd, $output, $return_var);