Port-foward command to access postgres from outside AKS not working in kubectl due to ampersand

5/31/2019

I am trying do deploy PostgreSQL on AKS from the following Bitnami chart: https://github.com/bitnami/charts/tree/master/upstreamed/postgresql/#installing-the-chart

During deployment, I have been invited to use the following command (once installed) to make sure I will be able to access postgres outside the cluster (e.g. some local DBMS)

kubectl port-forward --namespace default svc/dozing-coral-postgresql 5432:5432 & PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -p 5432

This results with a syntax error:

At line:1 char:80
+ ... d --namespace default svc/dozing-coral-postgresql 5432:5432 & PGPASSW 
...
+                                                                 ~
The ampersand (&) character is not allowed. The & operator is reserved for 
future use; wrap an ampersand in double quotation marks ("&") to pass it as 
part of a string.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmpersandNotAllowed

What would be the correct command to unclock the ports?

-- user2530062
azure-aks
bitnami
kubectl
kubernetes
postgresql

1 Answer

6/3/2019

The output you should see is something like

To connect to your database from outside the cluster execute the following commands:

    kubectl port-forward --namespace default svc/jolly-raccoon-postgresql 5432:5432 &
    PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -p 5432

There are two different commands. The first one is to forward the ports, and the final & is to send this command to the background so you are able to continue using the shell

$ kubectl port-forward --namespace default svc/jolly-raccoon-postgresql 5432:5432 &
[1] 62447
Forwarding from 127.0.0.1:5432 -> 5432

The second command allows you to connect to the database using the forwarded port from another host where you have installed the psql client

$ PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -p 5432
psql (11.3)
Type "help" for help.

postgres=#
-- Carlos Rodríguez Hernández
Source: StackOverflow