Skipoura Skipoura - 7 days ago 6
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:

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


Neither
@create user myusername;
nor
@/q
works.

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

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.