Synology DS415+ – Creating a PyCrypto Extensioned Postgres Instance for PostPooks

Written by James McDonald

May 13, 2015

Important Note: Completely ignore this post. See my post at

So I just got myself a Synology DS415+ diskstation NAS so I could get rid of my desktop PC. It has what appears to be a very modern Linux Kernel and an x64 atom CPU

synologybox> uname -a
Linux synologybox 3.2.40 #5022 SMP Wed Jan 7 14:19:49 CST 2015 x86_64 GNU/Linux synology_avoton_415+

However the default install of the postgres server doesn’t support the pycrypto extension which is require by Postbooks/xTuple

What follows is the most hacky way of getting an instance of postgress running on synology, that supports pycrypto, and I’m embarrassed at how bad it is

  1. On my Fedora 21 machine, install Postgres from source
    1. Remember to compile the contrib extensions
    2. run make install
  2. scp the resulting installation to the Synology box
    scp -r /usr/local/pgsql/9.3.6 root@synologybox:/volume1/folder1/pgsql
  3. Enable the Synology CLI and login to the synology box as root
    ssh -l root synologybox
  4. In the synology web interface create a user named postgres2 which will appear in /etc/passwd as: (in hindsight this should have been pguser or similar because postgres2 is too long for ls -al listings) you might need to vi /etc/passwd and change /sbin/nologin to /bin/sh
  5. Check for missing *.so (shared library) files and copy them from the F21 install to the lib folder
    export PATH=/volume1/folder1/pgsql/9.3.6/bin:$PATH
    export LD_LIBRARY_PATH=/volume1/folder1/pgsql/9.3.6/lib:$LD_LIBRARY_PATH
    # run postgres multiple times
    # that will spit out something link missing libpg etc etc
    # each time it errors out go to the fedora 21 system and copy the 
    # same file to the synology /volume1/folder1/pgsql/9.3.6/lib
    # e.g on fedora 21 repeat this command for each missing library
    scp /usr/lib64/ root@synologybox:/volume1/folder1/pgsql/9.3.6/lib
  6. Using the start script in postgresql-9.3.6/contrib/start-scripts/linux edit it so that it runs the new postgres instance as the correct user and contains the correct LD_LIBRARY_PATH which will pick up the missing shared libraries:
    # chkconfig: 2345 98 02
    # description: PostgreSQL RDBMS
    # This is an example of a start/stop script for SysV-style init, such
    # as is used on Linux systems.  You should edit some of the variables
    # and maybe the 'echo' commands.
    # Place this file at /etc/init.d/postgresql (or
    # /etc/rc.d/init.d/postgresql) and make symlinks to
    #   /etc/rc.d/rc0.d/K02postgresql
    #   /etc/rc.d/rc1.d/K02postgresql
    #   /etc/rc.d/rc2.d/K02postgresql
    #   /etc/rc.d/rc3.d/S98postgresql
    #   /etc/rc.d/rc4.d/S98postgresql
    #   /etc/rc.d/rc5.d/S98postgresql
    # Or, if you have chkconfig, simply:
    # chkconfig --add postgresql
    # Proper init scripts on Linux systems normally require setting lock
    # and pid files under /var/run as well as reacting to network
    # settings, so you should treat this with care.
    # Original author:  Ryan Kirkpatrick <[email protected]>
    # contrib/start-scripts/linux
    # change the port
    # Installation prefix
    # change prefix
    # add the correct library path and then edit the 
    # case statement below to include it for the su -c command
    # Data directory
    # change the data dir
    # Who to run the postmaster as, usually "postgres".  (NOT "root")
    # change the user
    # Where to keep a log file
    # It's often a good idea to protect the postmaster from being killed by the
    # OOM killer (which will tend to preferentially kill the postmaster because
    # of the way it accounts for shared memory).  Setting the OOM_SCORE_ADJ value
    # to -1000 will disable OOM kill altogether.  If you enable this, you probably
    # want to compile PostgreSQL with "-DLINUX_OOM_SCORE_ADJ=0", so that
    # individual backends can still be killed by the OOM killer.
    # Older Linux kernels may not have /proc/self/oom_score_adj, but instead
    # /proc/self/oom_adj, which works similarly except the disable value is -17.
    # For such a system, enable this and compile with "-DLINUX_OOM_ADJ=0".
    # The path that is to be used for the script
    # What to use to start up the postmaster.  (If you want the script to wait
    # until the server has started, you could use "pg_ctl start -w" here.
    # But without -w, pg_ctl adds no value.)
    # What to use to shut down the postmaster
    set -e
    # Only start if we can find the postmaster.
    test -x $DAEMON ||
    	echo "$DAEMON not found"
    	if [ "$1" = "stop" ]
    	then exit 0
    	else exit 5
    # Parse command line parameters.
    case $1 in
    	echo -n "Starting PostgreSQL: "
    	test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj
    	test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj
    	echo "ok"
    	echo -n "Stopping PostgreSQL: "
    	su - $PGUSER -c "LD_LIBRARY_PATH=$LD_LIBRARY_PATH $PGCTL stop -D '$PGDATA' -s -m fast"
    	echo "ok"
    	echo -n "Restarting PostgreSQL: "
    	su - $PGUSER -c "LD_LIBRARY_PATH=$LD_LIBRARY_PATH $PGCTL stop -D '$PGDATA' -s -m fast -w"
    	test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj
    	test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj
    	echo "ok"
            echo -n "Reload PostgreSQL: "
            su - $PGUSER -c "LD_LIBRARY_PATH=$LD_LIBRARY_PATH $PGCTL reload -D '$PGDATA' -s"
            echo "ok"
    	# Print help
    	echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2
    	exit 1
    exit 0
  7. copy the linux startup-script to /volume1/folder1/pgsql/9.3.6/bin and mark it executeable
  8. To put everything under the pgsql instance I created a data dir under the install prefix
    mkdir /volume1/folder1/pgsql/9.3.6/data
    chown postgres2 data -R
  9. Once you have everything ready you need to init the instance which creates all the necessary files (e.g. pg_hba.conf, postgresql)
    cd $prefix
  10. If initdb complains of PG_VERSION not existing in $prefix/data
    cd $prefix/data
    su - postgres2 -c "echo 9.3 > PG_VERSION"
  11. You need to allow postgres to listen on the network interface and remote md5 hashed password connections by editing pg_hba.conf
    # TYPE  DATABASE        USER            ADDRESS                 METHOD
    # "local" is for Unix domain socket connections only
    local   all             all                                     trust
    # IPv4 local connections:
    host    all             all               trust
    # IPv6 local connections:
    host    all             all             ::1/128                 trust
    # Allow replication connections from localhost, by a user with the
    # replication privilege.
    #local   replication     postgres2                                trust
    #host    replication     postgres2            trust
    #host    replication     postgres2        ::1/128                 trust
    # add this line
    host 	all		all		md5
  12. Once every thing is setup you can run your postgres instance
    synologybox> cd /volume1/folder1/pgsql/9.3.6
    synologybox> bin/linux
    Starting PostgreSQL: ok 
    synologybox> bin/linux status
    pg_ctl: server is running (PID: 16658) /volume1/folder1/pgsql/9.3.6/bin/postmaster "-D" "/volume1/folder1/pgsql/9.3.6/data" "-p" "5433"
  13. And from there create a new user
    You also need to create a superuser for postgres
    # need the correct env to run postgres
    createuser -h localhost -p 5433 --pwprompt --superuser --createdb --createrole admin
    # or easier with prompts
    createuser -h localhost -p 5433 --interactive admin
  14. Finally make sure that the potgres server starts at boot **I haven’t as yet tested if this works because I am doing a 3TB copy and it’s not finished and I can’t reboot the Synology
    synologybox> ln -sf /volume1/folder1/pgsql/9.3.6/bin/linux /usr/local/etc/rc.d/
  15. You will need to use something like pgadmin to restore your PostBooks db and create the pycrypto extension
  16. Finally you can test the Postbooks client from a remote host….

1 Comment

  1. Adrian Schiopu

    I want to install Postgresql 9.6.12 on my synology nas DS218+ (intel inside) as a second instance of PostgreSQL. preinstalled version is 9.3.22. (NOT docker version)
    I downloaded PostgreSQL from: and install it on /volume1/pgsql_9.6.12.
    Everything is ok until “initdb”.
    I got this error:

    root@PACS:/volume1/pgsql_9.6.12# sudo /bin/initdb
    Fail to read result of ‘/bin/get_key_value /etc/synoinfo.conf usbstation’
    initdb: no data directory specified
    You must identify the directory where the data for this database system
    will reside. Do this with either the invocation option -D or the
    environment variable PGDATA

    Can you help me to solve this problem?
    Thanks in advance



  1. Installing Postgres 9.x for Postbooks on Synology Diskstation with Docker | The Southern IT Observer - […] wrote a post about hacking another instance of Postgres onto the Synology Diskstation. But now that Synology has docker…

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...

Ubuntu on Hyper-v

It boils town to installing linux-azure # as root or sudo apt-get update apt-get install linux-azure...