From 98425a1680271388645b24a6615d949d674bd5d1 Mon Sep 17 00:00:00 2001 From: William Mantly Date: Mon, 27 May 2019 22:34:53 -0400 Subject: [PATCH] Added Postgres recipe --- attributes/postgres.rb | 5 +++++ recipes/postgress.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 attributes/postgres.rb create mode 100644 recipes/postgress.rb diff --git a/attributes/postgres.rb b/attributes/postgres.rb new file mode 100644 index 0000000..ff207c3 --- /dev/null +++ b/attributes/postgres.rb @@ -0,0 +1,5 @@ +require 'securerandom' + +default['db']['name'] = node['app']['name'] +default['db']['user'] = node['app']['name'] +default['db']['password'] = SecureRandom.hex(13) diff --git a/recipes/postgress.rb b/recipes/postgress.rb new file mode 100644 index 0000000..5e99705 --- /dev/null +++ b/recipes/postgress.rb @@ -0,0 +1,40 @@ +apt_update 'update' do +end.run_action(:update) if platform_family?('debian') + +postgresql_server_install 'My PostgreSQL Server install' do + initdb_locale 'en_US.utf8' + action :install +end + +postgresql_server_install 'Setup my PostgreSQL 9.6 server' do + initdb_locale 'en_US.utf8' + action :create +end + +postgresql_access 'local_postgres_superuser' do + comment 'Local postgres superuser access' + access_type 'local' + access_db 'all' + access_user 'postgres' + access_addr nil + access_method 'ident' +end + +postgresql_user 'DB user' do + create_user node['db']['user'] + password node['db']['password'] + createrole true +end + +# Hack for creating a database, this cook book is broken with debian... + +execute 'add database' do + command "createdb #{node['db']['name']}" + user 'postgres' + not_if "psql -lqt | grep -w \"#{node['db']['name']}\"", :user => 'postgres' +end + +execute 'Grant DB user' do + command "echo \"grant all privileges on database #{node['db']['name']} to #{node['db']['user']} ;\" | psql" + user 'postgres' +end