Getting error when installing postgres with existing /var/lib/postgresql
I want to install postgres (on debian) with an existing data directory at /var/lib/postgresql, but installation gives this error.
Setting up postgresql-13 (13.3-1.pgdg100+1) ...
Configuring already existing cluster (configuration: /etc/postgresql/13/main, data: /var/lib/postgresql/13/main, owner: 106:113)
Error: move_conffile: required configuration file /var/lib/postgresql/13/main/postgresql.conf does not exist
Error: could not create default cluster. Please create it manually with", "", " pg_createcluster 13 main --start", "", "or a similar command (see 'man pg_createcluster').
I'm provisioning a new system, so I don't want to restore the old /etc/postgresql along with the old /var/lib/postgresql. I want the postgres installation to create it from scratch like on a fresh installation, then my automated provisioning scripts will change the few settings that are different from the defaults. (I shouldn't have to bring along the old /etc/postgresql, since that could be bringing outdated configuration along with it if postgres has changed its defaults. Better to use the latest /etc/postgresql and just change what's needed.)
What's the best way to make it work? I could first install postgres without the old /var/lib/postgresql, then stop postgres, delete the newly created data dir, restore the old data dir, then start postgres. But this is more messy than it could be. Is there a way to force postgres to ignore /var/lib/postgresql and just recreate /etc/postgresql? Or any other options?
I'm surprised it isn't already possible since it's a common use case with cloud providers to attach an existing volume storage (which contains an existing /var/lib/postgresql) to a brand new server (which doesn't contain /etc/postgresql) and install postgres on that server.
Posted Jun 19, 2021, 5:56 PM UTC
You're best pointing the server to your cluser after a successful default install. Whether you do that by changing the config to point to the actual cluster location or by moving your cluster into the place expected by the default config is up to you. Either way, that's what I'd try.