title: How does the Postgres systemd unit file determine what version of Postgres to run?
body: <p>This is mainly a question out of curiosity. I was going through the Postgres systemd unit files to get a feel for what can be done with systemd. Postgres has two systemd unit files. One that is used in place of a systemd target:</p>
  
  <pre><code># systemd service for managing all PostgreSQL clusters on the system. This
  # service is actually a systemd target, but we are using a service since
  # targets cannot be reloaded.
  
  [Unit]
  Description=PostgreSQL RDBMS
  
  [Service]
  Type=oneshot
  ExecStart=/bin/true
  ExecReload=/bin/true
  RemainAfterExit=on
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>The second unit file is a template parameterized by on the Postgres version. It specifies it will be ran before the other unit file by using <code>Before=postgresql.service</code>:</p>
  
  <pre><code># systemd service template for PostgreSQL clusters. The actual instances will
  # be called "postgresql@version-cluster", e.g. "postgresql@9.3-main". The
  # variable %i expands to "version-cluster", %I expands to "version/cluster".
  # (%I breaks for cluster names containing dashes.)
  
  [Unit]
  Description=PostgreSQL Cluster %i
  ConditionPathExists=/etc/postgresql/%I/postgresql.conf
  PartOf=postgresql.service
  ReloadPropagatedFrom=postgresql.service
  Before=postgresql.service
  
  [Service]
  Type=forking
  # @: use "postgresql@%i" as process name
  ExecStart=@/usr/bin/pg_ctlcluster postgresql@%i --skip-systemctl-redirect %i start
  ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop
  ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reload
  PIDFile=/var/run/postgresql/%i.pid
  SyslogIdentifier=postgresql@%i
  # prevent OOM killer from choosing the postmaster (individual backends will
  # reset the score to 0)
  OOMScoreAdjust=-900
  # restarting automatically will prevent "pg_ctlcluster ... stop" from working,
  # so we disable it here. Also, the postmaster will restart by itself on most
  # problems anyway, so it is questionable if one wants to enable external
  # automatic restarts.
  #Restart=on-failure
  # (This should make pg_ctlcluster stop work, but doesn't:)
  #RestartPreventExitStatus=SIGINT SIGTERM
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>What I can't figure out is how systemd determines what versions of Postgres to run when I run <code>systemctl start postgresql</code>. When I look at the dependencies of <code>postgresql.service</code> I see there is a specific instance of the template, 9.5-main, that is a dependency:</p>
  
  <pre><code>&gt; systemctl list-dependencies postgresql
  
  postgresql.service
  ● ├─postgresql@9.5-main.service
  ● ├─postgresql@9.5-main.service
  ● ├─system.slice
  ● └─sysinit.target
  ●   ├─apparmor.service
  ●   ├─brltty.service
  ●   ├─console-setup.service
  ●   ├─dev-hugepages.mount
  ●   ├─dev-mqueue.mount
  ●   ├─friendly-recovery.service
  ●   ├─keyboard-setup.service
  ●   ├─kmod-static-nodes.service
  ●   ├─lvm2-lvmetad.socket
  ●   ├─lvm2-lvmpolld.socket
  ●   ├─lvm2-monitor.service
  ●   ├─plymouth-read-write.service
  ●   ├─plymouth-start.service
  ●   ├─proc-sys-fs-binfmt_misc.automount
  ●   ├─resolvconf.service
  ●   ├─setvtrgb.service
  ●   ├─sys-fs-fuse-connections.mount
  ●   ├─sys-kernel-config.mount
  </code></pre>
  
  <p>I can't find any file that specifies that Postgres 9.5 should be used. There aren't any other Postgres unit files and none of the other files mention Postgres at all. This is on Ubuntu 16.04 with systemd 229 and Postgres 9.5 (installed through <code>sudo apt-get install postgresql</code>).</p>