Sweetohm

Michel Casabianca


La version de PostgreSQL disponible avec une distribution Linux n’est pas toujours celle que l’on doit utiliser pour un projet. La solution est alors de compiler sa propre version de PostgreSQL.

Pour ce faire, on pourra suivre les étapes suivantes :

  • Télécharger les sources : https://www.postgresql.org/download/linux/debian/.
  • Détarrer et compiler le source :

    $ tar zxvf postgresql-9.5.12.tar.gz
    $ cd postgresql-9.5.12
    $ ./configure --prefix=/opt/misc/postgresql-9.5.12
    $ make
    $ sudo make install
    
  • Ajouer le ficher suivant dans le répertoire /etc/profile.d :

    #!/bin/sh
    # /etc/profile.d/psql.sh
    
    
    export PATH=/opt/misc/postgresql-9.5.12/bin:${PATH}
    
  • Taper les commandes suivantes pour créer la BDD :

    $ adduser postgres
    $ mkdir /home/data/psql
    $ chown postgres /home/data/psql
    $ su - postgres
    $ initdb -D /home/data/psql
    $ postgres -D /home/data/psql >logfile 2>&1 &
    $ createdb test
    $ psql test
    
  • Démarrer la BDD avec :

    $ pg_ctl -D /home/data/psql -l logfile start
    
  • Pour gérer la base de données avec systemd, placer dans le fichier /usr/lib/systemd/system/postgresql.service :

    [Unit]
    Description=PostgreSQL database server
    After=network.target
    
    
    [Service]
    Type=forking
    
    
    User=postgres
    Group=postgres
    
    # Where to send early-startup messages from the server (before the logging
    # options of postgresql.conf take effect)
    # This is normally controlled by the global default set by systemd
    # StandardOutput=syslog
    
    # Disable OOM kill on the postmaster
    OOMScoreAdjust=-1000
    # ... but allow it still to be effective for child processes
    # (note that these settings are ignored by Postgres releases before 9.5)
    Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
    Environment=PG_OOM_ADJUST_VALUE=0
    
    # Maximum number of seconds pg_ctl will wait for postgres to start.  Note that
    # PGSTARTTIMEOUT should be less than TimeoutSec value.
    Environment=PGSTARTTIMEOUT=270
    Environment=PGDATA=/home/data/psql
    
    
    ExecStart=/opt/misc/postgresql-9.5.12/bin/pg_ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT}
    ExecStop=/opt/misc/postgresql-9.5.12/bin/pg_ctl stop -D ${PGDATA} -s -m fast
    ExecReload=/opt/misc/postgresql-9.5.12/bin/pg_ctl reload -D ${PGDATA} -s
    
    # Give a reasonable amount of time for the server to start up/shut down.
    # Ideally, the timeout for starting PostgreSQL server should be handled more
    # nicely by pg_ctl in ExecStart, so keep its timeout smaller than this value.
    TimeoutSec=300
    
    
    [Install]
    WantedBy=multi-user.target
    

Il faudra bien sûr adapter cette procédure, qui installe PostgreSQL dans le répertoire /opt/misc/postgresql-x.y.z, les données dans /home/data/psql et tourne sous l’utilisateur postgres.

Cette procédure a été testée sous Debian 9.4.

Enjoy!