1<!-- doc/src/sgml/install-windows.sgml -->
2
3<chapter id="install-windows">
4 <title>Installation from Source Code on <productname>Windows</productname></title>
5
6 <indexterm>
7  <primary>installation</primary>
8  <secondary>on Windows</secondary>
9 </indexterm>
10
11 <para>
12  It is recommended that most users download the binary distribution for
13  Windows, available as a graphical installer package
14  from the <productname>PostgreSQL</productname> website. Building from source
15  is only intended for people developing <productname>PostgreSQL</productname>
16  or extensions.
17 </para>
18
19 <para>
20  There are several different ways of building PostgreSQL on
21  <productname>Windows</productname>. The simplest way to build with
22  Microsoft tools is to install <productname>Visual Studio 2019</productname>
23  and use the included compiler. It is also possible to build with the full
24  <productname>Microsoft Visual C++ 2013 to 2019</productname>.
25  In some cases that requires the installation of the
26  <productname>Windows SDK</productname> in addition to the compiler.
27 </para>
28
29 <para>
30  It is also possible to build PostgreSQL using the GNU compiler tools
31  provided by <productname>MinGW</productname>, or using
32  <productname>Cygwin</productname> for older versions of
33  <productname>Windows</productname>.
34 </para>
35
36 <para>
37  Building using <productname>MinGW</productname> or
38  <productname>Cygwin</productname> uses the normal build system, see
39  <xref linkend="installation"/> and the specific notes in
40  <xref linkend="installation-notes-mingw"/> and <xref linkend="installation-notes-cygwin"/>.
41  To produce native 64 bit binaries in these environments, use the tools from
42  <productname>MinGW-w64</productname>. These tools can also be used to
43  cross-compile for 32 bit and 64 bit <productname>Windows</productname>
44  targets on other hosts, such as <productname>Linux</productname> and
45  <productname>macOS</productname>.
46  <productname>Cygwin</productname> is not recommended for running a
47  production server, and it should only be used for running on
48  older versions of <productname>Windows</productname> where
49  the native build does not work, such as
50  <productname>Windows 98</productname>. The official
51  binaries are built using <productname>Visual Studio</productname>.
52 </para>
53
54 <para>
55  Native builds of <application>psql</application> don't support command
56  line editing. The <productname>Cygwin</productname> build does support
57  command line editing, so it should be used where psql is needed for
58  interactive use on  <productname>Windows</productname>.
59 </para>
60
61 <sect1 id="install-windows-full">
62  <title>Building with <productname>Visual C++</productname> or the
63  <productname>Microsoft Windows SDK</productname></title>
64
65 <para>
66  PostgreSQL can be built using the Visual C++ compiler suite from Microsoft.
67  These compilers can be either from <productname>Visual Studio</productname>,
68  <productname>Visual Studio Express</productname> or some versions of the
69  <productname>Microsoft Windows SDK</productname>. If you do not already have a
70  <productname>Visual Studio</productname> environment set up, the easiest
71  ways are to use the compilers from
72  <productname>Visual Studio 2019</productname> or those in the
73  <productname>Windows SDK 10</productname>, which are both free downloads
74  from Microsoft.
75 </para>
76
77 <para>
78  Both 32-bit and 64-bit builds are possible with the Microsoft Compiler suite.
79  32-bit PostgreSQL builds are possible with
80  <productname>Visual Studio 2013</productname> to
81  <productname>Visual Studio 2019</productname>,
82  as well as standalone Windows SDK releases 8.1a to 10.
83  64-bit PostgreSQL builds are supported with
84  <productname>Microsoft Windows SDK</productname> version 8.1a to 10 or
85  <productname>Visual Studio 2013</productname> and above. Compilation
86  is supported down to <productname>Windows 7</productname> and
87  <productname>Windows Server 2008 R2 SP1</productname> when building with
88  <productname>Visual Studio 2013</productname> to
89  <productname>Visual Studio 2019</productname>.
90   <!--
91       For 2013 requirements:
92       https://docs.microsoft.com/en-us/visualstudio/productinfo/vs2013-sysrequirements-vs
93       For 2015 requirements:
94       https://docs.microsoft.com/en-us/visualstudio/productinfo/vs2015-sysrequirements-vs
95       For 2017 requirements:
96       https://docs.microsoft.com/en-us/visualstudio/productinfo/vs2017-system-requirements-vs
97       For 2019 requirements:
98       https://docs.microsoft.com/en-us/visualstudio/releases/2019/system-requirements
99   -->
100 </para>
101
102 <para>
103  The tools for building using <productname>Visual C++</productname> or
104  <productname>Platform SDK</productname> are in the
105  <filename>src/tools/msvc</filename> directory. When building, make sure
106  there are no tools from <productname>MinGW</productname> or
107  <productname>Cygwin</productname> present in your system PATH. Also, make
108  sure you have all the required Visual C++ tools available in the PATH. In
109  <productname>Visual Studio</productname>, start the
110  <application>Visual Studio Command Prompt</application>.
111  If you wish to build a 64-bit version, you must use the 64-bit version of
112  the command, and vice versa.
113  Starting with <productname>Visual Studio 2017</productname> this can be
114  done from the command line using <command>VsDevCmd.bat</command>, see
115  <command>-help</command> for the available options and their default values.
116  <command>vsvars32.bat</command> is available in
117  <productname>Visual Studio 2015</productname> and earlier versions for the
118  same purpose.
119  From the <application>Visual Studio Command Prompt</application>, you can
120  change the targeted CPU architecture, build type, and target OS by using the
121  <command>vcvarsall.bat</command> command, e.g.,
122  <command>vcvarsall.bat x64 10.0.10240.0</command> to target Windows 10
123  with a 64-bit release build. See <command>-help</command> for the other
124  options of <command>vcvarsall.bat</command>. All commands should be run from
125  the <filename>src\tools\msvc</filename> directory.
126 </para>
127
128 <para>
129  Before you build, you may need to edit the file <filename>config.pl</filename>
130  to reflect any configuration options you want to change, or the paths to
131  any third party libraries to use. The complete configuration is determined
132  by first reading and parsing the file <filename>config_default.pl</filename>,
133  and then apply any changes from <filename>config.pl</filename>. For example,
134  to specify the location of your <productname>Python</productname> installation,
135  put the following in <filename>config.pl</filename>:
136<programlisting>
137$config->{python} = 'c:\python26';
138</programlisting>
139  You only need to specify those parameters that are different from what's in
140  <filename>config_default.pl</filename>.
141 </para>
142
143 <para>
144  If you need to set any other environment variables, create a file called
145  <filename>buildenv.pl</filename> and put the required commands there. For
146  example, to add the path for bison when it's not in the PATH, create a file
147  containing:
148<programlisting>
149$ENV{PATH}=$ENV{PATH} . ';c:\some\where\bison\bin';
150</programlisting>
151 </para>
152
153 <para>
154  To pass additional command line arguments to the Visual Studio build
155  command (msbuild or vcbuild):
156<programlisting>
157$ENV{MSBFLAGS}="/m";
158</programlisting>
159 </para>
160
161 <sect2>
162  <title>Requirements</title>
163  <para>
164   The following additional products are required to build
165   <productname>PostgreSQL</productname>. Use the
166   <filename>config.pl</filename> file to specify which directories the libraries
167   are available in.
168
169   <variablelist>
170    <varlistentry>
171     <term><productname>Microsoft Windows SDK</productname></term>
172     <listitem><para>
173      If your build environment doesn't ship with a supported version of the
174      <productname>Microsoft Windows SDK</productname> it
175      is recommended that you upgrade to the latest version (currently
176      version 10), available for download from
177      <ulink url="https://www.microsoft.com/download"></ulink>.
178     </para>
179     <para>
180      You must always include the
181      <application>Windows Headers and Libraries</application> part of the SDK.
182      If you install a <productname>Windows SDK</productname>
183      including the <application>Visual C++ Compilers</application>,
184      you don't need <productname>Visual Studio</productname> to build.
185      Note that as of Version 8.0a the Windows SDK no longer ships with a
186      complete command-line build environment.
187     </para></listitem>
188    </varlistentry>
189
190    <varlistentry>
191     <term><productname>ActiveState Perl</productname></term>
192     <listitem><para>
193      ActiveState Perl is required to run the build generation scripts. MinGW
194      or Cygwin Perl will not work. It must also be present in the PATH.
195      Binaries can be downloaded from
196      <ulink url="https://www.activestate.com"></ulink>
197      (Note: version 5.8.3 or later is required,
198      the free Standard Distribution is sufficient).
199     </para></listitem>
200    </varlistentry>
201
202   </variablelist>
203  </para>
204  <para>
205   The following additional products are not required to get started,
206   but are required to build the complete package. Use the
207   <filename>config.pl</filename> file to specify which directories the libraries
208   are available in.
209
210   <variablelist>
211    <varlistentry>
212     <term><productname>ActiveState TCL</productname></term>
213     <listitem><para>
214      Required for building <application>PL/Tcl</application> (Note: version
215      8.4 is required, the free Standard Distribution is sufficient).
216     </para></listitem>
217    </varlistentry>
218
219    <varlistentry>
220     <term><productname>Bison</productname> and
221      <productname>Flex</productname></term>
222     <listitem>
223     <para>
224      <productname>Bison</productname> and <productname>Flex</productname> are
225      required to build from Git, but not required when building from a release
226      file. Only <productname>Bison</productname> 1.875 or versions 2.2 and later
227      will work. <productname>Flex</productname> must be version 2.5.31 or later.
228     </para>
229
230     <para>
231      Both <productname>Bison</productname> and <productname>Flex</productname>
232      are included in the <productname>msys</productname> tool suite, available
233      from <ulink url="http://www.mingw.org/wiki/MSYS"></ulink> as part of the
234      <productname>MinGW</productname> compiler suite.
235     </para>
236
237     <para>
238      You will need to add the directory containing
239      <filename>flex.exe</filename> and <filename>bison.exe</filename> to the
240      PATH environment variable in <filename>buildenv.pl</filename> unless
241      they are already in PATH. In the case of MinGW, the directory is the
242      <filename>\msys\1.0\bin</filename> subdirectory of your MinGW
243      installation directory.
244     </para>
245
246     <note>
247      <para>
248        The Bison distribution from GnuWin32 appears to have a bug that
249        causes Bison to malfunction when installed in a directory with
250        spaces in the name, such as the default location on English
251        installations <filename>C:\Program Files\GnuWin32</filename>.
252        Consider installing into <filename>C:\GnuWin32</filename> or use the
253        NTFS short name path to GnuWin32 in your PATH environment setting
254        (e.g., <filename>C:\PROGRA~1\GnuWin32</filename>).
255       </para>
256     </note>
257
258     <note>
259       <para>
260       The obsolete <literal>winflex</literal> binaries distributed on the PostgreSQL FTP site
261       and referenced in older documentation will fail with <quote>flex: fatal
262       internal error, exec failed</quote> on 64-bit Windows hosts. Use Flex from
263       MSYS instead.
264       </para>
265     </note>
266     </listitem>
267    </varlistentry>
268
269    <varlistentry>
270     <term><productname>Diff</productname></term>
271     <listitem><para>
272      Diff is required to run the regression tests, and can be downloaded
273      from <ulink url="http://gnuwin32.sourceforge.net"></ulink>.
274     </para></listitem>
275    </varlistentry>
276
277    <varlistentry>
278     <term><productname>Gettext</productname></term>
279     <listitem><para>
280      Gettext is required to build with NLS support, and can be downloaded
281      from <ulink url="http://gnuwin32.sourceforge.net"></ulink>. Note that binaries,
282      dependencies and developer files are all needed.
283     </para></listitem>
284    </varlistentry>
285
286    <varlistentry>
287     <term><productname>MIT Kerberos</productname></term>
288     <listitem><para>
289      Required for GSSAPI authentication support. MIT Kerberos can be
290      downloaded from
291      <ulink url="https://web.mit.edu/Kerberos/dist/index.html"></ulink>.
292     </para></listitem>
293    </varlistentry>
294
295    <varlistentry>
296     <term><productname>libxml2</productname> and
297      <productname>libxslt</productname></term>
298     <listitem><para>
299      Required for XML support. Binaries can be downloaded from
300      <ulink url="https://zlatkovic.com/pub/libxml"></ulink> or source from
301      <ulink url="http://xmlsoft.org"></ulink>. Note that libxml2 requires iconv,
302      which is available from the same download location.
303     </para></listitem>
304    </varlistentry>
305
306    <varlistentry>
307     <term><productname>OpenSSL</productname></term>
308     <listitem><para>
309      Required for SSL support. Binaries can be downloaded from
310      <ulink url="https://slproweb.com/products/Win32OpenSSL.html"></ulink>
311      or source from <ulink url="https://www.openssl.org"></ulink>.
312     </para></listitem>
313    </varlistentry>
314
315    <varlistentry>
316     <term><productname>ossp-uuid</productname></term>
317     <listitem><para>
318      Required for UUID-OSSP support (contrib only). Source can be
319      downloaded from
320      <ulink url="http://www.ossp.org/pkg/lib/uuid/"></ulink>.
321     </para></listitem>
322    </varlistentry>
323
324    <varlistentry>
325     <term><productname>Python</productname></term>
326     <listitem><para>
327      Required for building <application>PL/Python</application>. Binaries can
328      be downloaded from <ulink url="https://www.python.org"></ulink>.
329     </para></listitem>
330    </varlistentry>
331
332    <varlistentry>
333     <term><productname>zlib</productname></term>
334     <listitem><para>
335      Required for compression support in <application>pg_dump</application>
336      and <application>pg_restore</application>. Binaries can be downloaded
337      from <ulink url="https://www.zlib.net"></ulink>.
338     </para></listitem>
339    </varlistentry>
340
341   </variablelist>
342  </para>
343 </sect2>
344
345 <sect2>
346  <title>Special Considerations for 64-Bit Windows</title>
347
348  <para>
349   PostgreSQL will only build for the x64 architecture on 64-bit Windows, there
350   is no support for Itanium processors.
351  </para>
352
353  <para>
354   Mixing 32- and 64-bit versions in the same build tree is not supported.
355   The build system will automatically detect if it's running in a 32- or
356   64-bit environment, and build PostgreSQL accordingly. For this reason, it
357   is important to start the correct command prompt before building.
358  </para>
359
360  <para>
361   To use a server-side third party library such as <productname>python</productname> or
362   <productname>OpenSSL</productname>, this library <emphasis>must</emphasis> also be
363   64-bit. There is no support for loading a 32-bit library in a 64-bit
364   server. Several of the third party libraries that PostgreSQL supports may
365   only be available in 32-bit versions, in which case they cannot be used with
366   64-bit PostgreSQL.
367  </para>
368 </sect2>
369
370 <sect2>
371  <title>Building</title>
372
373  <para>
374   To build all of PostgreSQL in release configuration (the default), run the
375   command:
376<screen>
377<userinput>build</userinput>
378</screen>
379   To build all of PostgreSQL in debug configuration, run the command:
380<screen>
381<userinput>build DEBUG</userinput>
382</screen>
383   To build just a single project, for example psql, run the commands:
384<screen>
385<userinput>build psql</userinput>
386<userinput>build DEBUG psql</userinput>
387</screen>
388   To change the default build configuration to debug, put the following
389   in the <filename>buildenv.pl</filename> file:
390<programlisting>
391$ENV{CONFIG}="Debug";
392</programlisting>
393  </para>
394
395  <para>
396   It is also possible to build from inside the Visual Studio GUI. In this
397   case, you need to run:
398<screen>
399<userinput>perl mkvcbuild.pl</userinput>
400</screen>
401   from the command prompt, and then open the generated
402   <filename>pgsql.sln</filename> (in the root directory of the source tree)
403   in Visual Studio.
404  </para>
405 </sect2>
406
407 <sect2>
408  <title>Cleaning and Installing</title>
409
410  <para>
411   Most of the time, the automatic dependency tracking in Visual Studio will
412   handle changed files. But if there have been large changes, you may need
413   to clean the installation. To do this, simply run the
414   <filename>clean.bat</filename> command, which will automatically clean out
415   all generated files. You can also run it with the
416   <parameter>dist</parameter> parameter, in which case it will behave like
417   <userinput>make distclean</userinput> and remove the flex/bison output files
418   as well.
419  </para>
420
421  <para>
422   By default, all files are written into a subdirectory of the
423   <filename>debug</filename> or <filename>release</filename> directories. To
424   install these files using the standard layout, and also generate the files
425   required to initialize and use the database, run the command:
426<screen>
427<userinput>install c:\destination\directory</userinput>
428</screen>
429  </para>
430
431  <para>
432   If you want to install only the client applications and
433   interface libraries, then you can use these commands:
434<screen>
435<userinput>install c:\destination\directory client</userinput>
436</screen>
437  </para>
438 </sect2>
439
440 <sect2>
441  <title>Running the Regression Tests</title>
442
443  <para>
444   To run the regression tests, make sure you have completed the build of all
445   required parts first. Also, make sure that the DLLs required to load all
446   parts of the system (such as the Perl and Python DLLs for the procedural
447   languages) are present in the system path. If they are not, set it through
448   the <filename>buildenv.pl</filename> file. To run the tests, run one of
449   the following commands from the <filename>src\tools\msvc</filename>
450   directory:
451<screen>
452<userinput>vcregress check</userinput>
453<userinput>vcregress installcheck</userinput>
454<userinput>vcregress plcheck</userinput>
455<userinput>vcregress contribcheck</userinput>
456<userinput>vcregress modulescheck</userinput>
457<userinput>vcregress ecpgcheck</userinput>
458<userinput>vcregress isolationcheck</userinput>
459<userinput>vcregress bincheck</userinput>
460<userinput>vcregress recoverycheck</userinput>
461<userinput>vcregress upgradecheck</userinput>
462</screen>
463
464   To change the schedule used (default is parallel), append it to the
465   command line like:
466<screen>
467<userinput>vcregress check serial</userinput>
468</screen>
469
470   For more information about the regression tests, see
471   <xref linkend="regress"/>.
472  </para>
473
474  <para>
475   Running the regression tests on client programs, with
476   <command>vcregress bincheck</command>, or on recovery tests, with
477   <command>vcregress recoverycheck</command>, requires an additional Perl module
478   to be installed:
479   <variablelist>
480    <varlistentry>
481     <term><productname>IPC::Run</productname></term>
482     <listitem><para>
483      As of this writing, <literal>IPC::Run</literal> is not included in the
484      ActiveState Perl installation, nor in the ActiveState Perl Package
485      Manager (PPM) library. To install, download the
486      <filename>IPC-Run-&lt;version&gt;.tar.gz</filename> source archive from CPAN,
487      at <ulink url="https://metacpan.org/release/IPC-Run"></ulink>, and
488      uncompress. Edit the <filename>buildenv.pl</filename> file, and add a PERL5LIB
489      variable to point to the <filename>lib</filename> subdirectory from the
490      extracted archive. For example:
491<programlisting>
492$ENV{PERL5LIB}=$ENV{PERL5LIB} . ';c:\IPC-Run-0.94\lib';
493</programlisting>
494     </para></listitem>
495    </varlistentry>
496   </variablelist>
497  </para>
498 </sect2>
499
500 </sect1>
501</chapter>
502