Blog by Daniel Hauck. Sysadmin, Developer, Blogger.

What would be a modern web application without a database? Ok, you could also build an API that just takes data modifies it and give it back to you. Or a static site generator or whatever. But in most cases, you will build apps with databases.

Today I want to show you, how to install PostgreSQL with ansible. We will also cover how to create databases and users for the apps. Have fun!

Install PostgreSQL

As always, we first need to create our role:

Next let’s create our mainfile and install it with apt, as we are used to:

We use the latest available version here, so on trusty it should be 9.3 I think.

Second we need the python postgres binding, for creating users and adding databases to the postgres cluster. Therefore we first need pip, libpq-dev and python-dev. After that then install psycopg2.

Yes, it’s that easy.

Databases and Users

Now we are able to create users and databases. And of course, we do this from our apps from group_vars. So let’s create the variables first in provisioning/group_vars/all: … domain: db_user: symfony db_name: symfony db_passwd: secret …

And now use them, like a boss 😉

Another thing, you should recognize here, is the become parameter. Haven’t you ever wanted to be someone else? At least in ansible it’s that easy, just use become_user to become whoever you want.

That’s it

Don’t forget to add your role to playbook.yml:

and provision your machine:

We have installed postgres and created users, and a database for our project. In the next part, we will finally install symfony and put all together! See you on the other side!

There are no comments.

Leave a Reply