Skip to content

Instantly share code, notes, and snippets.

@yokawasa
Created September 7, 2022 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yokawasa/5358e79636d480274b8731a050ff5a98 to your computer and use it in GitHub Desktop.
Save yokawasa/5358e79636d480274b8731a050ff5a98 to your computer and use it in GitHub Desktop.
Running Postgres Client in Kubernetes

First, create postgresql-client pod

cat << EOF | kubectl apply -f - 
---
apiVersion: v1
kind: Namespace
metadata:
  name: postgresql-client

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: postgresql-client
  namespace: postgresql-client

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: postgresql-client
  namespace: postgresql-client

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: postgresql-client
  namespace: postgresql-client
roleRef:
  kind: Role
  name: postgresql-client
  apiGroup: rbac.authorization.k8s.io
subjects:
  - kind: ServiceAccount
    name: postgresql-client

---
apiVersion: v1
kind: Pod
metadata:
  name: postgresql-client
  namespace: postgresql-client
  labels:
    app: postgresql-client
  annotations:
    cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
spec:
  serviceAccountName: postgresql-client
  securityContext:
    runAsNonRoot: true
    supplementalGroups: [ 10001]
    fsGroup: 10001
  containers:
    - name: postgresql-client
      image: andreswebs/postgresql-client
      imagePullPolicy: Always
      securityContext:
        runAsUser: 1000
      stdin: true
      tty: true
      command: ["/bin/sh"]
EOF

then, attach to the pod

kubectl attach --namespace=postgresql-client -ti postgresql-client

postgres> psql -h <host_ip_address> -d <dbname> -p <port> -U <user>

references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment