Simplify port calculation in postgres_get_port and add immediate failure

Immediate failure is added with "set -e" at the top of the script.
Calculation is refactored as well to fit with this need.