Skipoura Skipoura - 1 year ago 75
Linux Question

LINUX GNU Create New user Postgres DB in Makefile

I'm new with Makefile. I need to automate create user for postgres terminal.

I tried this example and it did not work for me:

@echo "$(OK_COLOR)==> create user$(NO_COLOR)"
@psql mydbdevelopmentname
@create user myusername;
@echo "$(OK_COLOR)==> Done$(NO_COLOR)"

@create user myusername;

When I ran
make createuser
and exit manually with \q from postgres terminal, I receive this error:

/bin/sh: create: command not found
make: *** [createuser] Error 127

Answer Source

When debugging makefiles, the @ modifier is unhelpful, as it no longer shows those commands before executing them.

I suspect that you actually wanted to pass create user myusername to your psql line, but you wrote it as a separate command. You need to make sure that psql sees it:

psql -c "create user myusername" mydbdevelopmentname

Don't forget to declare your target as a dependency of .PHONY.

I'll note in passing that Make is generally a poor choice to use as a scripting language - it's much better for making targets from their dependencies, as it's designed for. This kind of thing is more suited to a language such as Bash or Python.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download