【问题标题】:Setup basic postgres database using Chef使用 Chef 设置基本的 postgres 数据库
【发布时间】:2020-04-06 02:32:14
【问题描述】:

我正在尝试使用 Chef 建立一个非常基本的 postgres 数据库,但缺乏 Chef 经验,无法理解我在超市的食谱自述文件中找到的文档: https://supermarket.chef.io/cookbooks/postgresql

我需要的是相当于以下的厨师:

  • 创建用户
  • 创建该用户拥有的数据库
  • 对 pg_hba.conf 稍作更改,使其需要密码
  • 启动数据库

我希望能够在谷歌上搜索一个与此类似的基本示例,但目前在配置 postgresql 的所有可能方式中迷失了方向,而我没有找到起点。 如果另一本食谱更有意义,请解释原因。

【问题讨论】:

    标签: postgresql chef-infra chef-recipe


    【解决方案1】:

    我们目前没有一个很好的答案来制作 DB 用户和数据库。旧的 database 食谱曾经处理过这个问题,但代码是如此不可维护,我们觉得有义务在没有替换计划的情况下弃用它。它仍然存在并且应该可以正常工作,但这不是一个令人愉快的体验 :( 对于 hba 配置,您可以使用 postgresql 食谱和一些节点属性来控制它,并且类似地会为您处理启动数据库服务。

    【讨论】:

      【解决方案2】:

      我从这里给出的 Gist 那里得到了帮助:https://gist.github.com/exoer/1063712/01e9b166add182c14b54508ef5950152063f6de2

      #
      # Cookbook Name:: atari
      # Recipe:: db_server
      #
      # Copyright 2010, Atari
      #
      # All rights reserved
      #
      
      include_recipe "postgresql::server"
      
      r = package "ruby-pg" do
        package_name "libpgsql-ruby"
        action :nothing
      end
      r.run_action(:upgrade)
      
      # Snippet from opscode to reload gems
      require 'rubygems'
      Gem.clear_paths
      require "pg"
      
      execute "create-database-user" do
        command "createuser -U postgres -SDRw youruser"
        only_if do
          c = PGconn.connect(:user=>'postgres', :dbname=>'postgres')
          r = c.exec("SELECT COUNT(*) FROM pg_user WHERE usename='youruser'")
          r.entries[0]['count']
        end
      end
      
      execute "create-database" do
        command "createdb -U postgres -O youruser-E utf8 -T template0 yourdb"
        only_if do
          c = PGconn.connect(:user=>'postgres', :dbname=>'postgres')
          r = c.exec("SELECT COUNT(*) FROM pg_database WHERE datname='yourdb'")
          r.entries[0]['count']
        end
      end
      

      还检查了 postgres 文档:https://www.postgresql.org/docs/9.5/app-createdb.html

      最后我编辑了我的 dev-cookbook/recipes/db.rb 并创建了用户

      # d - user is allowed to create database
      # l - user is allowed to log in
      # s - user is a superuser
      execute "create-database-user" do
        command "createuser -U postgres -dls db_user"
      end
      

      数据库

      # U - user that is used to connect to psql
      # O - owner of the database
      # E - database encoding
      execute "create-database" do
        command "createdb -U postgres -O db_user db_name -E 'utf8'"
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 2014-07-11
        • 1970-01-01
        • 2013-11-26
        • 1970-01-01
        • 2011-08-16
        相关资源
        最近更新 更多