在Chef Solo中安装PostgreSQL并允许其他主机连接

PostgreSQL安装后默认只允许本地主机连接,因此需要保存从其他主机连接的步骤。

执行环境

    • chef 11.8.2

 

    CentOS 6.3

创建烹饪书

创建一个名为”postgres”的菜谱。

knife cookbook create postgres -o site-cookbooks

食谱 (shí pǔ)

在「site-cookbooks/postgres/recipes/default.rb」中写入以下内容。

安装PostgreSQL

package "postgresql-server" do
     action :install
end

数据库的初始化

执行初始化命令。
执行命令后,将生成“/var/lib/pgsql/data”目录,因此可以通过判断data目录是否存在来确定是否已经执行过。

dataDir = "/var/lib/pgsql/data/"
if not File.exists? dataDir then
   execute "postgresql-init" do
      command "service postgresql initdb"
   end
end

修改配置文件

修改配置文件以允许外部连接。

template "/var/lib/pgsql/data/pg_hba.conf" do
   mode 0600
end

template "/var/lib/pgsql/data/postgresql.conf" do
   mode 0600
end

設定文件讀取和自動啟動設置

由于更改了配置文件,需要重新启动postgresql。
为了打开端口,需要禁用iptables。

service "postgresql" do
    action [:enable, :restart]
end

service "iptables" do
   action [:disable, :stop]
end

模板

postgresql.conf 是一个用于配置 PostgreSQL 数据库的配置文件。

将“postgresql.conf.erb”文件作为“site-cookbooks/postgres/templates/default”的配置文件放置。

他允许来自主机的连接,并明确指定端口。

listen_addresses ='*'
port = 5432         # (change requires restart)

這是 “pg_hba.conf” 的中文翻譯。

将「site-cookbooks/postgres/templates/default」中的「pg_hba.conf.erb」文件放置在其中。
在所有主机的连接中,保持使用密码认证的设置。

# IPv4 local connections:
host    all     all       0.0.0.0/0        password

以上,请将配方和模板放置在以下的github上。

广告
将在 10 秒后关闭
bannerAds