Skip to content

Instantly share code, notes, and snippets.

@clarkdave
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkdave/9786643 to your computer and use it in GitHub Desktop.
Save clarkdave/9786643 to your computer and use it in GitHub Desktop.
(CHEF) change PostgreSQL (9.2) data directory
# changing the postgresql data directory is an annoyingly non-trivial and fragile operation
# this should mostly do it but there are lots of things that can go wrong and if/when they do
# you will have lots of fun trying to eradicate broken postgres installs between chef runs
#
config_dir = "etc/postgresql/#{node[:postgresql][:version]}/main"
bash 'change_data_directory' do
code <<-CODE
mkdir /tmp/pg_conf_backup
cp #{config_dir}/*.conf /tmp/pg_conf_backup/
pg_dropcluster --stop 9.2 main
pg_createcluster --locale=en_US.utf8 9.2 -d /data/postgresql main
cp /tmp/pg_conf_backup/*.conf #{config_dir}/
chmod 0600 #{config_dir}/postgresql.conf #{config_dir}/pg_hba.conf
sed -i -e"s:data_directory.*$:data_directory = '/data/postgresql':" #{config_dir}/postgresql.conf
CODE
not_if {
::FileTest.exist?(File.join('/data/postgresql', 'PG_VERSION'))
}
notifies :create, 'ruby_block[set_data_directory]', :immediately
end
ruby_block 'set_data_directory' do
block do
# we forcibly set the data directory for this node here, because we couldn't set it
# earlier or it'd break the first start of postgresql (for goodness sake!)
node.set[:postgresql][:config][:data_directory] = '/data/postgresql'
end
action :nothing
notifies :start, 'service[postgresql]', :immediately
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment