Search code examples
postgresqlubuntudockervagrant

automation script for postgresql


Could you please help how can I run automation script for postgresql after installing postgresql on Ubuntu? I need to automatize DB preparation before using it (create table, insert data, alter permissions).

I need to do it with the current rights of the user. E.g., I have the only user admin_ubuntu. He has all rights to run psql. All scripts are written but how can I run the script? usually, I need to edit configs ( I believe, /etc/postgresql/9.1/main/pg_hba.conf). But I do not want to do it.

So, what I need is just to run sql which will make a lot of things. How can I do it to run it? The problem is that I need to do a lot of things to allow to run sql when the OS is immaculate (empty).

It will be made every time when the Ubuntu will have been installed.


Solution

  • you should be able to run shell provisioning - following is example of what you can do:

    # 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 mydevdb
    
    # 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