What is a reliable way to change PostgreSQL configuration, especially move its data directory in a script?

StackOverflow https://stackoverflow.com/questions/22174860

Pergunta

What I want to Achieve

For packaging some software together with PostgreSQL in a docker container, I want to change PostgreSQL's data_directory together with all other persistent data to /srv.

Background information:

  • PostgreSQL is freshly set up using apt-get
  • the postgresql service was never started at that point
  • My aim is to move have the persistent data in /srv without getting in trouble later. I don't care for how this is achieved, setting data_directory just seems most appropriate to me.

Thoughts on the Problem

  • I could just use sed to modify postgresql.conf, for example like this:

    $ sed -i 's#^\(data_directory\s*=\s*\).*$#\1/srv/postgresql/main#'\
      /etc/postgresql/9.1/main/postgresql.conf
    

    But I don't really like messing around in configuration files with scripts if there is a better way. I found a rather huge perl script for modifying PostgreSQL configuration files, but I'm somewhat scared to embed this without being able to fully understand what it's doing in five minutes.

  • I'm not surprised that changing from within a running system using psql and the SET SQL command fails:

    postgres=# set data_directory to '/srv/postgresql/main';
    ERROR:  parameter "data_directory" cannot be changed without restarting the server
    
  • pg_config on the other hand is only able to print information and not installed anyway.

And now?

Often, there are convenient and reliable tools to set options, does PostgreSQL also ship one?

Foi útil?

Solução

Since you mention apt-get as the way of installing, it means we're talking about PostgreSQL as packaged for Debian and its derivatives.

In Debian, the PostgreSQL data location is set by creating a cluster like this:

# pg_createcluster --datadir=/path/to/data [other options] version name

where version is a major version as in 9.1 and name a short name of your choice identifiying this cluster. It actually puts data structures inside the directory, as opposed to just changing a line in postgresql.conf which by itself would be useless.

When running apt-get install postgresql or a variant implying to install a new PG server, it implicitly creates and starts a cluster whose name is main and datadir is /usr/lib/postgresql/<version>/main

You want to remove that cluster before creating another one to have only one cluster running, example with 9.1:

# pg_dropcluster --stop 9.1 main
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top