{
  "date": "2017-08-01T04:00:50",
  "slug": "postgresql-on-cygwin",
  "link": "https://www.dbi-services.com/blog/postgresql-on-cygwin/",
  "title": {
    "rendered": "PostgreSQL on Cygwin"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nI run my laptop with Windows 10 for office programs, and VirtualBox machines with Linux for the big stuff (Oracle databases). I have also Cygwin installed on Windows for GNU programs. I wanted to quickly install PosgreSQL and rather than installing it in a Linux VM, or as a Windows program, I installed the Cygwin version of it. Here is how.<br />\n<!--more--></p>\n<h3>Cygwin</h3>\n<p>Cygwin is easy to install: just run the setup-x86_64.exe from <a href=\"https://www.cygwin.com/\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.cygwin.com/</a> and choose the packages you want to install. Here is what is related to PostgreSQL:<br />\n<a href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0001.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0001.png\" alt=\"CapturePGCY0001\" width=\"829\" height=\"451\" class=\"alignnone size-full wp-image-17403\" /></a></p>\n<p>Note that if you want to install postgres extensions you may need pg_config and you need to install the libpd-devel in addition to postgresql-devel. And gcc and make. Those are not displayed in the screenshot above but you may get something like the following, if you don&#8217;t have them, when installing an extension:</p>\n<pre><code>pg_config: Command not found</code></pre>\n<p>Of course, PostgreSQL is Open Source and you can also compile it yourself.</p>\n<h3>Cygserver</h3>\n<p>Cygwin can run daemons through a Windows service (Cygserver) and you need to set it up if not already done. For this step, you will need to run the Cygwin Terminal as Administrator.</p>\n<pre><code>fpa@dell-fpa ~\n$ /usr/bin/cygserver-config\nOverwrite existing /etc/cygserver.conf file? (yes/no) yes\nGenerating /etc/cygserver.conf file\n&nbsp;\nWarning: The following function requires administrator privileges!\n&nbsp;\nDo you want to install cygserver as service?\n(Say \"no\" if it's already installed as service) (yes/no) yes\n&nbsp;\nThe service has been installed under LocalSystem account.\nTo start it, call `net start cygserver' or `cygrunsrv -S cygserver'.\n&nbsp;\nFurther configuration options are available by editing the configuration\nfile /etc/cygserver.conf.  Please read the inline information in that\nfile carefully. The best option for the start is to just leave it alone.\n&nbsp;\nBasic Cygserver configuration finished. Have fun!\n</code></pre>\n<p>You start this service as any Windows service:</p>\n<pre><code>fpa@dell-fpa ~\n$ net start cygserver\nThe CYGWIN cygserver service is starting.\nThe CYGWIN cygserver service was started successfully.</code></pre>\n<p>You can check from that the service is running:</p>\n<pre><code>\nfpa@dell-fpa ~\n$ cygstart services.msc\n</code></pre>\n<p><a href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0002.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0002.png\" alt=\"CapturePGCY0002\" width=\"975\" height=\"688\" class=\"alignnone size-full wp-image-17406\" /></a></p>\n<h3>PostgreSQL database cluster</h3>\n<p>Here is the creation of the PostgreSQL database cluster.</p>\n<pre><code>fpa@dell-fpa ~\n$ /usr/sbin/initdb -D /usr/share/postgresql/data\nThe files belonging to this database system will be owned by user \"fpa\".\nThis user must also own the server process.\n&nbsp;\nThe database cluster will be initialized with locale \"C\".\nThe default database encoding has accordingly been set to \"SQL_ASCII\".\nThe default text search configuration will be set to \"english\".\n&nbsp;\nData page checksums are disabled.\n&nbsp;\ncreating directory /usr/share/postgresql/data ... ok\ncreating subdirectories ... ok\nselecting default max_connections ... 30\nselecting default shared_buffers ... 128MB\nselecting dynamic shared memory implementation ... posix\ncreating configuration files ... ok\nrunning bootstrap script ... ok\nperforming post-bootstrap initialization ... ok\nsyncing data to disk ... ok\n&nbsp;\nWARNING: enabling \"trust\" authentication for local connections\nYou can change this by editing pg_hba.conf or using the option -A, or\n--auth-local and --auth-host, the next time you run initdb.\n&nbsp;\nSuccess. You can now start the database server using:\n&nbsp;\n    /usr/sbin/pg_ctl -D /usr/share/postgresql/data -l log.txt start</code></pre>\n<h3>Start PostgreSQL database server</h3>\n<p>I add my network onto the /usr/share/postgresql/data/pg_hba.conf</p>\n<pre><code># IPv4 local connections:\nhost    all             all             127.0.0.1/32            trust\nhost    all             all             192.168.78.0/24         trust</code></pre>\n<p>I define the interface and port where the server listen in /usr/share/postgresql/data/postgresql.conf</p>\n<pre><code>listen_addresses = 'localhost,192.168.78.1'     # what IP address(es) to listen on;\n                                        # comma-separated list of addresses;\n                                        # defaults to 'localhost'; use '*' for all\n                                        # (change requires restart)\nport = 5432                             # (change requires restart)\nmax_connections = 30                    # (change requires restart)</code></pre>\n<p>Now ready to start the PostgreSQL server:</p>\n<pre><code>fpa@dell-fpa ~\n$     /usr/sbin/pg_ctl -D /usr/share/postgresql/data -l log.txt start\nserver starting</code></pre>\n<h3>Username</h3>\n<p>My Windows username is &#8216;FPA&#8217; and so is the Cygwin user which started the database server and I check that I can connect to the maintenance database with this user:</p>\n<pre><code>fpa@dell-fpa ~\n$ psql -U fpa postgres\npsql (9.6.2)\nType \"help\" for help.\n&nbsp;\npostgres=# \\du\n&nbsp;\n                                   List of roles\n Role name |                         Attributes                         | Member of\n-----------+------------------------------------------------------------+-----------\n fpa       | Superuser, Create role, Create DB, Replication, Bypass RLS | {}\n&nbsp;\npostgres=# quit</code></pre>\n<h3>PgAdmin</h3>\n<p>As I am on Windows, I install the graphical console PgAdmin and setup the connection to this database:<br />\n<a href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0003.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0003.png\" alt=\"CapturePGCY0003\" width=\"1019\" height=\"629\" class=\"alignnone size-full wp-image-17414\" /></a></p>\n<h3>SQL Developer</h3>\n<p>As an Oracle fan, I prefer to connect with SQL Developer. Just download the JDBC driver for PostgreSQL: <a href=\"https://jdbc.postgresql.org/download.html\" target=\"_blank\" rel=\"noopener noreferrer\">https://jdbc.postgresql.org/download.html</a></p>\n<p>In SQL Developer you can declare this .jar from Tools -&gt; Preferences -&gt; Third Party JDBC Drivers</p>\n<p><a href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0004.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0004.png\" alt=\"CapturePGCY0004\" width=\"694\" height=\"502\" class=\"alignnone size-large wp-image-17419\" /></a></p>\n<p>And create the connection with the new &#8216;PostgreSQL&#8217; tab:</p>\n<p><a href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0005.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0005.png\" alt=\"CapturePGCY0005\" width=\"605\" height=\"300\" class=\"alignnone size-full wp-image-17418\" /></a><br />\nThen with &#8216;Choose Database&#8217; you can fill the dropbox and choose the database you want to connect to.</p>\n<p>As I have no database with the same name as the username, I have to mention the database name at the end of the hostname, suffixed with &#8216;?&#8217; to get the proper JDBC url. And what you put in the dropbox will be ignored. I don&#8217;t really know the reason, but this is how I got the correct url.</p>\n<p><a href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0006.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CapturePGCY0006.png\" alt=\"CapturePGCY0006\" width=\"261\" height=\"214\" class=\"alignnone size-full wp-image-17422\" /></a></p>\n<h3>Extensions</h3>\n<p>You can install extensions. For example, I&#8217;ve installed pg_hint_plan to be able to hint the access path and join methods.</p>\n<pre><code>\nwget https://osdn.net/dl/pghintplan/pg_hint_plan96-1.2.1.tar.gz\ntar -zxvf pg_hint_plan96-1.2.1.tar.gz\ncd pg_hint_plan96-1.2.1\nmake\nmake install\n</code></pre>\n<p>And I&#8217;m now able to load it:</p>\n<pre><code>\n$ psql\npsql (9.6.2)\nType \"help\" for help.\n&nbsp;\nfpa=# load 'pg_hint_plan';\nLOAD\n</code></pre>\n<h3>But Why?</h3>\n<p>You may wonder why I don&#8217;t install it directly on Linux. My laptop is on Windows and, of course, I have a lot of VirtualBox VMs. But this doesn&#8217;t require to start a VM.<br />\nYou may wonder why I don&#8217;t install the Windows version? I want to investigate the linux behaviour. And I may want to trace the postgres processes. For example, cygwin has a strace.exe which shows similar output as strace on Linux. Here is the I/O calls from a full table scan (Seq Scan):<br />\n<a href=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CaptureStraceCygwinPostgres.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https://www.dbi-services.com/blog/wp-content/uploads/sites/2/2022/04/CaptureStraceCygwinPostgres.png\" alt=\"CaptureStraceCygwinPostgres\" width=\"1024\" height=\"295\" class=\"alignnone size-large wp-image-18004\" /></a><br />\nI can see that postgres sequential reads are done through one lseek() and sequential 8k read().</p>\n<p>This was simple. Just get the pid of the session process:</p>\n<pre><code>\nfpa=# select pg_backend_pid();\n pg_backend_pid\n----------------\n          11960\n</code></pre>\n<p>and strace it:</p>\n<pre><code>\n$ strace -p 11960\n</code></pre>\n<p>I&#8217;ve done that in about one hour: download, install, setup and write this blog post. Without any virtual machine, you can have a Linux Postgres database server running on Windows.</p>\n",
    "protected": false
  }
}
