Browse Source

fix: Use the specified port on socket connection

Emmanuel Bouthenot 3 weeks ago
parent
commit
30e760454c
2 changed files with 12 additions and 8 deletions
  1. 2 2
      Documentation.md
  2. 10 6
      autopostgresqlbackup

+ 2 - 2
Documentation.md

@@ -26,13 +26,13 @@ Username to access the PostgreSQL server
 
 ### `DBHOST`
 
-Host name (or IP address) of PostgreSQL server
+Host name (or IP address) of PostgreSQL server. Use `localhost` for socket connection or `127.0.0.1` to force TCP connection.
 
 **default**: `localhost`
 
 ### `DBPORT`
 
-Port of PostgreSQL server (only used if `${DBHOST} != localhost`).
+Port of PostgreSQL server. It is also used if `${DBHOST}` is `localhost` (socket connection) as socket name contains port.
 
 **default**: `5432`
 

+ 10 - 6
autopostgresqlbackup

@@ -49,10 +49,13 @@ USERNAME="postgres"
 # replace hostname with the value of ${DBHOST}, dbuser with the value of
 # ${USERNAME} and dbpass with the password.
 
-# Host name (or IP address) of PostgreSQL server
+# Host name (or IP address) of PostgreSQL server.
+# Use 'localhost' for socket connection or '127.0.0.1' to force TCP connection
 DBHOST="localhost"
 
-# Port of PostgreSQL server (only used if ${DBHOST} != localhost).
+# Port of PostgreSQL server.
+# It is also used if ${DBHOST} is localhost (socket connection) as socket name
+# contains port
 DBPORT="5432"
 
 # List of database(s) names(s) to backup If you would like to backup all
@@ -200,9 +203,9 @@ if [[ "${HOSTNAME}" != *.* ]]; then
     HOSTNAME="$(hostname --fqdn)"
 fi
 
-HOST="${HOSTNAME}"
-if [ "${DBHOST}" != "localhost" ]; then
-    HOST="${HOSTNAME}:${DBPORT}"
+HOST="${DBHOST}:${DBPORT}"
+if [ "${DBHOST}" = "localhost" ]; then
+    HOST="${HOSTNAME}:${DBPORT} (socket)"
 fi
 
 CONN_ARGS=()
@@ -299,8 +302,9 @@ compression () {
 
 # {{{ pgdb_init()
 pgdb_init () {
+    CONN_ARGS=(--port "${DBPORT}")
     if [ "${DBHOST}" != "localhost" ]; then
-        CONN_ARGS=(--host "${DBHOST}" --port "${DBPORT}")
+        CONN_ARGS+=(--host "${DBHOST}")
     fi
     if [ -n "${USERNAME}" ]; then
         CONN_ARGS+=(--username "${USERNAME}")