saltstack/salt — pull request #2212 by jonasvp (2012-10-10 09:01:08+00:00)
Title: Use the createdb command instead of psql

I'm starting to get into Salt and liking it... however, I'm ran into walls trying to create a postgres database (Debian squeeze, Salt 0.10.1 from backports). Running this command I get an (very unspecific) error:

salt-call -l debug postgres_backport.db_create gt1 runas=postgres owner=gt1 encoding=UTF8

Stepping through the code, I see that the command being executed is this:

su -s /bin/bash - postgres -c "cd /var/lib/postgresql && psql --no-align --no-readline --no-password -c \'CREATE DATABASE gt1 WITH ENCODING \'"\'"\'UTF8\'"\'"\' OWNER = gt1\'"

Which seems to be a couple of quotes too many. I tracked it down to pipes.quote() (here: https://github.com/saltstack/salt/blob/develop/salt/modules/postgres.py#L84), which doesn't seem to deal gracefully with single quotes within the parameters.

I haven't been able to fix it there and ended up changing db_create() to use the "createdb" command instead of running "psql". I'm not sure how portable the "createdb" command is, however, and would be happy to see a better way of fixing this.

Jonas