LoginSignup
1
1

More than 5 years have passed since last update.

CakePHPからPostgreSQLのpsqlコマンドを実行する方法

Last updated at Posted at 2015-09-11

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);
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1