Installing Drush and using it with Postgres on windows

Installing Drush and having it work with Postgres on windows is a real head-ache. These are the instructions to get Drush and Postgres on windows running.

Install composer

Install Drush composer

  • Open a command line.
  • Go to a directory where you want to install Drush.
  • Run "composer require drush/drush:8.x" to have composer start install drush.

Set PATH windows environment variable

  • Search for 'system enivronments' in windows 10.
  • Click on 'Environment Variables'.
  • Click on 'Path' and then 'edit'.
  • Add the directory where the drush executable is located. This is the drush\vendor\drush\drush subdirectory where dr.bat is located.
  • Add the directory where your database executable is located.  For Mysql this is the mysql\bin subdirectory which contains mysql.exe. For Postgres this is the C:\Program Files\PostgreSQL\[version]\bin directory which contains the postgres.exe and psql.exe executables.

Patch drush when using Postgres

The postgres php file of Drush needs to be patched so a database command to execute postgres is created that is correct in windows.
 

  • Go to the directory you installed Drush in.
  • Edit the \vendor\drush\drush\lib\Drush\Sql\Sqlpgsql.php file.

Change line 9, from:
public $query_extra = "--no-align --field-separator='\t' --pset tuples_only=on";

To:
public $query_extra = "--no-align --pset tuples_only=on";

Change lline 45, from
$environment = "PGPASSFILE={$pw_file} ";

To
//$environment = "PGPASSFILE={$pw_file} ";

 This way a correct database command is constructed.
 

Set Postgres password

Since the password was removed from the database command we need to set it in the windows enviroment variables.

  • Search for 'system enivronments' in windows 10.
  • Click on 'Environment Variables'.
  • Under 'System variables' create a new variable named PGPASSWORD and set the postgres database password as the value.

Use Drush

You can now use command in your sites root directory by running dr on the command line: dr cc all.

References