How to rename a PostgreSQL cluster?

Update: version 166 of the postgresl-common package added pg_renamecluster. See Ilya's new answer about that.


In older versions (including Postgres 9.1.4 on Ubuntu 11.10) the name of a cluster is bound to the name of the data directory in the "Multi-Version/Multi-Cluster PostgreSQL architecture" introduced by Martin Pitt. This is shipped with Debian packages (Debian, Ubuntu and related distributions). For a detailed description of the architecture, see:

/usr/share/doc/postgresql-common/README.Debian.gz

Or browse the file online here.

Other installations do not have all of these features, so some people may be confused when you talk about cluster names.

Just renaming the data directory does not work, because you have to change the directory in/etc/postgresql/<version>/<cluster> accordingly.

And there are a couple of settings in your postgresql.conf, that have to be adjusted, too. Run a grep in the directory to see:

postgres@db:/etc/postgresql/9.1/main$ grep 'main' ./*

Debian/Ubuntu has a pg_renamecluster command (available in "postgresql-common" package) which does just what you need:

  • renames config and data directory,
  • renames existing log files,
  • changes a number of settings in the postgresql.conf.

Man page.

Note for Systemd users: generators are used to create unit file (i.e. service) for each PostgreSQL cluster. So after the pg_renamecluster you should reload Systemd config, enable and start new service: sudo systemctl daemon-reload sudo systemctl enable postgresql@<version>-<new cluster name>.service sudo systemctl start postgresql