如何从 vagrantfile 设置 postgres 配置

How to setup postgres config from vagrantfile

我想创建一个 postgres 用户,然后创建一个具有特定模式规范的 table。有没有办法通过 Vagrantfile?

我的 Vagrantfile 有内联配置脚本。

我尝试在 google 上搜索,但所有可用的内容都与我不会使用的 chefansiblepuppet 相关。

重申我需要能够创建没有密码的数据库用户和具有某种模式的 table。

如果我问的对你来说没有意义,请纠正我。我对 devops 东西一窍不通。

我的 Vagrant 文件:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.define "twitter-vm"
  config.vm.box = "ubuntu/trusty64"

  config.vm.box_check_update = false

  # config.vm.network "forwarded_port", guest: 80, host: 8080

  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder "./", "/home/vagrant/app"

  config.vm.provider "virtualbox" do |vb|
     vb.memory = "512"
  end

  config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"

  config.vm.provision "shell", privileged: false, inline: <<-SHELL
    sudo apt-get -y update
    sudo apt-get install -y git-core curl zlib1g-dev build-essential 
    sudo apt-get install -y libssl-dev libreadline-dev libyaml-dev 
    sudo apt-get install -y libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev 
    sudo apt-get install -y libcurl4-openssl-dev python-software-properties libffi-dev
    sudo apt-get install -y postgresql postgresql-client postgresql-contrib libpq-dev
    git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
    sudo locale-gen en_US en_US.UTF-8
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc    
    echo 'eval "$(rbenv init -)"' >> ~/.bashrc
    git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
    echo 'export LC_CTYPE=en_US.UTF-8' >> ~/.bashrc
    echo 'export LC_ALL=en_US.UTF-8' >> ~/.bashrc
    source ~/.bashrc    
    ~/.rbenv/bin/rbenv install 2.2.2
    ~/.rbenv/bin/rbenv global 2.2.2
    ~/.rbenv/bin/rbenv rehash
    ~/.rbenv/shims/gem install bundler
    cd ~/app
    ~/.rbenv/shims/bundle install
    SHELL
end

您应该可以通过添加以下行继续使用您的 shell 配置:

# creating user
sudo -u postgres psql -c "CREATE USER admin WITH PASSWORD 'password';"
# creating new db if needed .. might need 2 (dev/test)
createdb -U vagrant swg_dev

# you can generate table from ruby (looks like you use ruby)
rake db:migrate
rake db:test:prepare

# if you have more complex things you'll need to put that in a create_db.sql file and run the script as
sudo -u postgres psql < create_db.sql

create_db.sql 文件可以包含任何 CREATE TABLE 语句