OwnBlog from DimiG    About    Archive    RSS

PostgreSQL under Cygwin

As I wrote below I use Cygwin with Windows platform to achieve the Busi-do 😏. Yeah, I’m not looking for easy ways, really. To use the Cygwin is not easy way, but it really works for many tasks. If you work with databases there are two ways. First is to setup database directly into the Windows, the second is setup database into the Cygwin. The first way is easiest one, but may NOT working if you develop inside Cygwin.

The AIM of this article to describe how to setup the PostgreSQL database into the Cygwin.

At first you need setup PostgreSQL by Cygwin setup-x86_64.exe. Don’t forget to install development package. If you program by Python and you need access the database and setup the psycopg2 don’t forget to install the libpq-devel.

Add the /usr/sbin and $HOME/bin in your PATH setting.

The next step install the Windows service by:

/usr/bin/cygserver-config

After this you can run it by standard Windows service management console. But I don’t recommend run the service automatically when Windows start. We will do it manually.

Next enable the PostgreSQL in Cygwin by:

$ cygrunsrv -S cygserver
$ /usr/sbin/initdb -D /usr/share/postgresql/data
$ /usr/sbin/pg_ctl start -D /usr/share/postgresql/data \
-l /var/log/postgresql.log
$ createdb

I use the start/stop script which I hold in $HOME/bin called pgres.sh:

#!/usr/bin/bash
CYGWIN=server
CYGSERVER=/usr/sbin/cygserver
PGDATA=/usr/share/postgresql/data
PGCTL=/usr/sbin/pg_ctl
PGLOG=/var/log/postgresql.log

usage() {
    echo "USAGE: pgres (start|stop|restart|reload|status)"
    echo
}

start() {
    echo "starting postgresql..."
    $CYGSERVER -E & 2>&1
    sleep 3
    $PGCTL start -D $PGDATA -l $PGLOG
    echo
}

stop() {
    echo "stopping postgresql..."
    $PGCTL stop -D $PGDATA  -m smart
    $CYGSERVER -S 2>&1
    sleep 3
    echo
}

restart() {
    echo "restarting postgresql..."
    $PGCTL restart -D $PGDATA  -m smart
    echo
}

reload() {
    echo "reloading postgresql..."
    $PGCTL reload -D $PGDATA
    echo
}


status() {
    $PGCTL status -D $PGDATA
}

if [ -n "$1" ]
then
    case $1 in
    start)
        start
        exit 0
        ;;
    stop)
        stop
        exit 0
        ;;
    restart)
        restart
        exit 0
        ;;
    reload)
        reload
        exit 0
        ;;
    status)
        status
        exit 0
        ;;
    *)
        usage
        exit 1
        ;;
    esac
else
    usage
    exit 1
fi

Check out if the CYGWIN cygserver is stopped and won’t run automatically. It may prevent to function properly if you run database server manually.

If any questions write me in comments below.

Happy New 2016 Year for Everyone!