Skip to content
This repository has been archived by the owner on Jan 23, 2019. It is now read-only.

Commit

Permalink
support explicit postgresql.conf templating
Browse files Browse the repository at this point in the history
  • Loading branch information
phlipper committed Feb 3, 2013
1 parent fd3fce6 commit 394bbc1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 32 deletions.
94 changes: 63 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ The following platforms are supported by this cookbook, meaning that the recipes

This cookbook installs the postgresql components if not present, and pulls updates if they are installed on the system.

Additionally this cookbook provides three definitions to create, alter and delete users as well as create and drop databases or setup extensions. Usage is as follows:
This cookbook provides three definitions to create, alter, and delete users as well as create and drop databases, or setup extensions. Usage is as follows:


### Users

```ruby
# create a user
pg_user "myuser" do
Expand All @@ -60,7 +62,27 @@ end
pg_user "myuser" do
action :drop
end
```

Or add users via attributes:

```json
"postgresql": {
"users": [
{
"username": "dickeyxxx",
"password": "password",
"superuser": true,
"createdb": true,
"login": true
}
]
}
```

### Databases and Extensions

```ruby
# create a database
pg_database "mydb" do
owner "myuser"
Expand Down Expand Up @@ -88,46 +110,57 @@ pg_database "mydb" do
end
```

Or add the user/database via attributes:
Or add the database via attributes:

```ruby
:users => [
{
:username => "dickeyxxx",
:password => "password",
:superuser => true,
:createdb => true,
:login => true
}
],

:databases => [
{
:name => "my_db",
:owner => "dickeyxxx",
:template => "template0",
:encoding => "utf8",
:locale => "en_US.UTF8",
:extensions => "hstore"
```json
"postgresql": {
"databases": [
{
"name": "my_db",
"owner": "dickeyxxx",
"template": "template0",
"encoding": "utf8",
"locale": "en_US.UTF8",
"extensions": "hstore"
}
]
}
```

### Configuration

The `postgresql.conf` configuration may be set one of two ways:

* set individual node attributes to be interpolated into the default template
* create a custom configuration hash to write a custom file

To create a custom configuration, set the `node["postgresql"]["conf"]` hash with your custom settings:

```json
"postgresql": {
"conf": {
"data_directory": "/dev/null",
// ... all options explicitly set here
}
]
}
```

Or add contents to the pg_hba.conf via attributes:
You may also set the contents of `pg_hba.conf` via attributes:

```ruby
```json
"postgresql": {
"pg_hba": [
{ type: "local", db: "all", user: "postgres", addr: "", method: "ident" },
{ type: "local", db: "all", user: "all", addr: "", method: "trust" },
{ type: "host", db: "all", user: "all", addr: "127.0.0.1/32", method: "trust" },
{ type: "host", db: "all", user: "all", addr: "::1/128", method: "trust" },
{ type: "host", db: "all", user: "postgres", addr: "127.0.0.1/32", method: "trust" },
{ type: "host", db: "all", user: "username", addr: "127.0.0.1/32", method: "trust" }
{ "type": "local", "db": "all", "user": "postgres", "addr": "", "method": "ident" },
{ "type": "local", "db": "all", "user": "all", "addr": "", "method": "trust" },
{ "type": "host", "db": "all", "user": "all", "addr": "127.0.0.1/32", "method": "trust" },
{ "type": "host", "db": "all", "user": "all", "addr": "::1/128", "method": "trust" },
{ "type": "host", "db": "all", "user": "postgres", "addr": "127.0.0.1/32", "method": "trust" },
{ "type": "host", "db": "all", "user": "username", "addr": "127.0.0.1/32", "method": "trust" }
]
}
```


## Attributes

```ruby
Expand Down Expand Up @@ -495,7 +528,6 @@ default["postgresql"]["custom_variable_classes"] = ""
## TODO

* Add support for replication setup
* Add support for custom config files
* Add installation and configuration for the following packages:

```
Expand Down
6 changes: 5 additions & 1 deletion recipes/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@
end

# postgresql
pg_template_source = node["postgresql"]["conf"].any? ? "custom" : "standard"
template "/etc/postgresql/#{pg_version}/main/postgresql.conf" do
source "postgresql.conf.erb"
source "postgresql.conf.#{pg_template_source}.erb"
owner "postgres"
group "postgres"
mode "0644"
variables(:configuration => node["postgresql"]["conf"])
notifies :restart, "service[postgresql]"
end

Expand All @@ -85,13 +87,15 @@
notifies :restart, "service[postgresql]", :immediately
end

# setup users
node["postgresql"]["users"].each do |user|
pg_user user["username"] do
privileges :superuser => user["superuser"], :createdb => user["createdb"], :login => user["login"]
password user["password"]
end
end

# setup databases
node["postgresql"]["databases"].each do |database|
pg_database database["name"] do
owner database["owner"]
Expand Down
10 changes: 10 additions & 0 deletions templates/default/postgresql.conf.custom.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# generated by Chef for node <%= node["fqdn"] %>
#
# -----------------------------
# PostgreSQL configuration file
# -----------------------------
#

<% @configuration.each_pair do |key, value| %>
<%= "#{key} = '#{value}'" %>
<% end %>

0 comments on commit 394bbc1

Please sign in to comment.