|  | 
|  | 1 | +--- | 
|  | 2 | +title: "Connect to a Postgres Cluster" | 
|  | 3 | +draft: false | 
|  | 4 | +weight: 120 | 
|  | 5 | +--- | 
|  | 6 | + | 
|  | 7 | +Naturally, once the [PostgreSQL cluster is created]({{< relref "tutorial/create-cluster.md" >}}), you may want to connect to it. You can get the credentials of the users of the cluster using the [`pgo show user`]({{< relref "pgo-client/reference/pgo_show_user.md" >}}) command, i.e.: | 
|  | 8 | + | 
|  | 9 | +``` | 
|  | 10 | +pgo show user hippo | 
|  | 11 | +``` | 
|  | 12 | + | 
|  | 13 | +yields output similar to: | 
|  | 14 | + | 
|  | 15 | +``` | 
|  | 16 | +CLUSTER USERNAME PASSWORD EXPIRES STATUS ERROR | 
|  | 17 | +------- -------- -------------------------------- ------- ------ ----- | 
|  | 18 | +hippo testuser securerandomlygeneratedpassword never ok | 
|  | 19 | +``` | 
|  | 20 | + | 
|  | 21 | +If you need to get the password of one of the system or privileged accounts, you will need to use the `--show-system-accounts` flag, i.e.: | 
|  | 22 | + | 
|  | 23 | +``` | 
|  | 24 | +pgo show user hippo --show-system-accounts | 
|  | 25 | +``` | 
|  | 26 | + | 
|  | 27 | +``` | 
|  | 28 | +CLUSTER USERNAME PASSWORD EXPIRES STATUS ERROR | 
|  | 29 | +------- ----------- -------------------------------- ------- ------ -----  | 
|  | 30 | +hippo postgres B>xy}9+7wTVp)gkntf}X|H@N never ok  | 
|  | 31 | +hippo primaryuser ^zULckQy-\KPws:2UoC+szXl never ok  | 
|  | 32 | +hippo testuser securerandomlygeneratedpassword never ok | 
|  | 33 | +``` | 
|  | 34 | + | 
|  | 35 | +Let's look at three different ways we can connect to the PostgreSQL cluster. | 
|  | 36 | + | 
|  | 37 | +## Connecting via `psql` | 
|  | 38 | + | 
|  | 39 | +Let's see how we can connect to `hippo` using [`psql`](https://www.postgresql.org/docs/current/app-psql.html), the command-line tool for accessing PostgreSQL. Ensure you have [installed the `psql` client](https://www.crunchydata.com/developers/download-postgres/binaries/postgresql12). | 
|  | 40 | + | 
|  | 41 | +The PostgreSQL Operator creates a service with the same name as the cluster. See for yourself! Get a list of all of the Services available in the `pgo` namespace: | 
|  | 42 | + | 
|  | 43 | +``` | 
|  | 44 | +kubectl -n pgo get svc | 
|  | 45 | +
 | 
|  | 46 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE | 
|  | 47 | +hippo ClusterIP 10.96.218.63 <none> 2022/TCP,5432/TCP 59m | 
|  | 48 | +hippo-backrest-shared-repo ClusterIP 10.96.75.175 <none> 2022/TCP 59m | 
|  | 49 | +postgres-operator ClusterIP 10.96.121.246 <none> 8443/TCP,4171/TCP,4150/TCP 71m | 
|  | 50 | +``` | 
|  | 51 | + | 
|  | 52 | +Let's connect the `hippo` cluster. First, in a different console window, set up a port forward to the `hippo` service: | 
|  | 53 | + | 
|  | 54 | +``` | 
|  | 55 | +kubectl -n pgo port-forward svc/hippo 5432:5432 | 
|  | 56 | +``` | 
|  | 57 | + | 
|  | 58 | +You can connect to the database with the following command, substituting `datalake` for your actual password: | 
|  | 59 | + | 
|  | 60 | +``` | 
|  | 61 | +PGPASSWORD=datalake psql -h localhost -p 5432 -U testuser hippo | 
|  | 62 | +``` | 
|  | 63 | + | 
|  | 64 | +You should then be greeted with the PostgreSQL prompt: | 
|  | 65 | + | 
|  | 66 | +``` | 
|  | 67 | +psql ({{< param postgresVersion >}}) | 
|  | 68 | +Type "help" for help. | 
|  | 69 | +
 | 
|  | 70 | +hippo=> | 
|  | 71 | +``` | 
|  | 72 | + | 
|  | 73 | +## Connecting via [pgAdmin 4]({{< relref "architecture/pgadmin4.md" >}}) | 
|  | 74 | + | 
|  | 75 | +[pgAdmin 4]({{< relref "architecture/pgadmin4.md" >}}) is a graphical tool that can be used to manage and query a PostgreSQL database from a web browser. The PostgreSQL Operator provides a convenient integration with pgAdmin 4 for managing how users can log into the database. | 
|  | 76 | + | 
|  | 77 | +To add pgAdmin 4 to `hippo`, you can execute the following command: | 
|  | 78 | + | 
|  | 79 | +``` | 
|  | 80 | +pgo create pgadmin -n pgo hippo | 
|  | 81 | +``` | 
|  | 82 | + | 
|  | 83 | +It will take a few moments to create the pgAdmin 4 instance. The PostgreSQL Operator also creates a pgAdmin 4 service. See for yourself! Get a list of all of the Services available in the `pgo` namespace: | 
|  | 84 | + | 
|  | 85 | +``` | 
|  | 86 | +kubectl -n pgo get svc | 
|  | 87 | +
 | 
|  | 88 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE | 
|  | 89 | +hippo ClusterIP 10.96.218.63 <none> 2022/TCP,5432/TCP 59m | 
|  | 90 | +hippo-backrest-shared-repo ClusterIP 10.96.75.175 <none> 2022/TCP 59m | 
|  | 91 | +hippo-pgadmin ClusterIP 10.96.165.27 <none> 5050/TCP 5m1s | 
|  | 92 | +postgres-operator ClusterIP 10.96.121.246 <none> 8443/TCP,4171/TCP,4150/TCP 71m | 
|  | 93 | +``` | 
|  | 94 | + | 
|  | 95 | +Let's connect to our `hippo` cluster via pgAdmin 4! In a different terminal, set up a port forward to pgAdmin 4: | 
|  | 96 | + | 
|  | 97 | +``` | 
|  | 98 | +kubectl -n pgo port-forward svc/hippo-pgadmin 5050:5050 | 
|  | 99 | +``` | 
|  | 100 | + | 
|  | 101 | +Navigate your browser to http://localhost:5050 and use your database username (`testuser`) and password (e.g. `datalake`) to log in. Though the prompt says “email address”, using your PostgreSQL username will work: | 
|  | 102 | + | 
|  | 103 | + | 
|  | 104 | + | 
|  | 105 | +(There are occasions where the initial credentials do not properly get set in pgAdmin 4. If you have trouble logging in, try running the command `pgo update user -n pgo hippo --username=testuser --password=datalake`). | 
|  | 106 | + | 
|  | 107 | +Once logged into pgAdmin 4, you will be automatically connected to your database. Explore pgAdmin 4 and run some queries! | 
|  | 108 | + | 
|  | 109 | +## Connecting from a Kubernetes Application | 
|  | 110 | + | 
|  | 111 | +### Within a Kubernetes Cluster | 
|  | 112 | + | 
|  | 113 | +Connecting a Kubernetes application that is within the same cluster that your PostgreSQL cluster is deployed in is as simple as understanding the default [Kubernetes DNS system](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#what-things-get-dns-names). A cluster created by the PostgreSQL Operator automatically creates a Service of the same name (e.g. `hippo`). | 
|  | 114 | + | 
|  | 115 | +Following the example we've created, the hostname for our PostgreSQL cluster is `hippo.pgo` (or `hippo.pgo.svc.cluster.local`). To get your exact [DNS resolution rules](https://kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution/), you may need to consult with your Kubernetes administrator. | 
|  | 116 | + | 
|  | 117 | +Knowing this, we can construct a [Postgres URI](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) that contains all of the connection info: | 
|  | 118 | + | 
|  | 119 | +`postgres://testuser:securerandomlygeneratedpassword@hippo.jkatz.svc.cluster.local:5432/hippo` | 
|  | 120 | + | 
|  | 121 | +which breaks down as such: | 
|  | 122 | + | 
|  | 123 | +- `postgres`: the scheme, i.e. a Postgres URI | 
|  | 124 | +- `testuser`: the name of the PostgreSQL user | 
|  | 125 | +- `securerandomlygeneratedpassword`: the password for `testuser` | 
|  | 126 | +- `hippo.jkatz.svc.cluster.local`: the hostname | 
|  | 127 | +- `5432`: the port | 
|  | 128 | +- `hippo`: the database you want to connect to | 
|  | 129 | + | 
|  | 130 | +If your application or connection driver cannot use the Postgres URI, the above should allow for you to break down the connection string into its appropriate components. | 
|  | 131 | + | 
|  | 132 | +### Outside a Kubernetes Cluster | 
|  | 133 | + | 
|  | 134 | +To connect to a database from an application that is outside a Kubernetes cluster, you will need to set one of the following: | 
|  | 135 | + | 
|  | 136 | +- A Service type of [`LoadBalancer`](https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer) or [`NodePort`](https://kubernetes.io/docs/concepts/services-networking/service/#nodeport) | 
|  | 137 | +- An [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/). The PostgreSQL Operator does not provide any management for Ingress types. | 
|  | 138 | + | 
|  | 139 | +To have the PostgreSQL Operator create a Service that is of type `LoadBalancer` or `NodePort`, you can use the `--service-type` flag as part of creating a PostgreSQL cluster, e.g.: | 
|  | 140 | + | 
|  | 141 | +``` | 
|  | 142 | +pgo create cluster hippo --service-type=LoadBalancer | 
|  | 143 | +``` | 
|  | 144 | + | 
|  | 145 | +You can also set the `ServiceType` attribute of the [PostgreSQL Operator configuration]({{< relref "configuration/pgo-yaml-configuration.md" >}}) to provide a default Service type for all PostgreSQL clusters that are created. | 
|  | 146 | + | 
|  | 147 | +## Next Steps | 
|  | 148 | + | 
|  | 149 | +We've created a cluster and we've connected to it! Now, let's learn what customizations we can make as part of the cluster creation process. | 
0 commit comments