Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup postgres config from vagrantfile

I want to create a postgres user and then create a table with certain schema specification. Is there a way to do that through Vagrantfile?

My Vagrantfile has inline provisioning script.

I tried searching on google but everything available was related to chef or ansible or puppet which i will not be using.

Reiterating I need to be able to create db user with no password and a table with some schema.

Please correct me if what i am asking doesn't make sense to you. I am noob at devops stuff.

My Vagrant file :

# -*- 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
like image 453
vjdhama Avatar asked Dec 10 '22 21:12

vjdhama


1 Answers

you should be able to continue using your shell provisioning by adding the following lines:

# 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

The create_db.sql file can contain any CREATE TABLE statement

like image 153
Frederic Henri Avatar answered Dec 24 '22 10:12

Frederic Henri