This image contains an installation of PostgreSQL 10.x.
For more information, see the Official Image Launcher Page.
Pull command (first install gcloud):
gcloud docker -- pull launcher.gcr.io/google/postgresql10Dockerfile for this image can be found here.
Consult Launcher container documentation for additional information about setting up your Kubernetes environment.
This section describes how to spin up a PostgreSQL service using this image.
Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.
apiVersion: v1
kind: Pod
metadata:
name: some-postgres
labels:
name: some-postgres
spec:
containers:
- image: launcher.gcr.io/google/postgresql10
name: postgres
env:
- name: "POSTGRES_PASSWORD"
value: "example-password"Run the following to expose the port. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.
kubectl expose pod some-postgres --name some-postgres-5432 \
--type LoadBalancer --port 5432 --protocol TCPFor information about how to retain your database across restarts, see Use a persistent data volume.
We can store PostgreSQL data on a persistent volume. This way the database remains intact across restarts.
Copy the following content to pod.yaml file, and run kubectl create -f pod.yaml.
apiVersion: v1
kind: Pod
metadata:
name: some-postgres
labels:
name: some-postgres
spec:
containers:
- image: launcher.gcr.io/google/postgresql10
name: postgres
env:
- name: "POSTGRES_PASSWORD"
value: "example-password"
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/data
subPath: data
volumes:
- name: data
persistentVolumeClaim:
claimName: data
---
# Request a persistent volume from the cluster using a Persistent Volume Claim.
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: data
annotations:
volume.alpha.kubernetes.io/storage-class: default
spec:
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 5GiRun the following to expose the port. Depending on your cluster setup, this might expose your service to the Internet with an external IP address. For more information, consult Kubernetes documentation.
kubectl expose pod some-postgres --name some-postgres-5432 \
--type LoadBalancer --port 5432 --protocol TCPThis section describes how to use this image as a PostgreSQL client.
You can run a PostgreSQL client directly within the container.
kubectl exec -it some-postgres -- psql --username postgresNote: No password is required when connecting from inside the same container.
Assume that we have a PostgreSQL server running at some-host and we want to log on to some-db database as postgres user. Run the following command. You will need to enter the password even though there might be no visible passowrd prompt; this is due to limitations of kubectl exec.
kubectl run \
some-postgres-client \
--image launcher.gcr.io/google/postgresql10 \
--rm --attach --restart=Never \
-it \
-- sh -c 'exec psql --host some-host --dbname some-db --username postgres --password'All databases can be dumped into a /some/path/all-databases.sql file on the host using the following command.
kubectl exec -it some-postgres -- sh -c 'exec pg_dumpall --username postgres' > /some/path/all-databases.sqlConsult Launcher container documentation for additional information about setting up your Docker environment.
This section describes how to spin up a PostgreSQL service using this image.
Use the following content for the docker-compose.yml file, then run docker-compose up.
version: '2'
services:
postgres:
container_name: some-postgres
image: launcher.gcr.io/google/postgresql10
environment:
"POSTGRES_PASSWORD": "example-password"
ports:
- '5432:5432'Or you can use docker run directly:
docker run \
--name some-postgres \
-e "POSTGRES_PASSWORD=example-password" \
-p 5432:5432 \
-d \
launcher.gcr.io/google/postgresql10The PostgreSQL server is accessible on port 5432.
For information about how to retain your database across restarts, see Use a persistent data volume.
We can store PostgreSQL data on a persistent volume. This way the database remains intact across restarts. Assume that /my/persistent/dir/postgres is the persistent directory on the host.
Use the following content for the docker-compose.yml file, then run docker-compose up.
version: '2'
services:
postgres:
container_name: some-postgres
image: launcher.gcr.io/google/postgresql10
environment:
"POSTGRES_PASSWORD": "example-password"
ports:
- '5432:5432'
volumes:
- /my/persistent/dir/postgres:/var/lib/postgresql/dataOr you can use docker run directly:
docker run \
--name some-postgres \
-e "POSTGRES_PASSWORD=example-password" \
-p 5432:5432 \
-v /my/persistent/dir/postgres:/var/lib/postgresql/data \
-d \
launcher.gcr.io/google/postgresql10This section describes how to use this image as a PostgreSQL client.
You can run a PostgreSQL client directly within the container.
docker exec -it some-postgres psql --username postgresNote: No password is required when connecting from inside the same container.
Assume that we have a PostgreSQL server running at some-host and we want to log on to some-db database as postgres user. Run the following command.
All databases can be dumped into a /some/path/all-databases.sql file on the host using the following command.
docker exec -it some-postgres sh -c 'exec pg_dumpall --username postgres' > /some/path/all-databases.sqlThese are the ports exposed by the container image.
| Port | Description |
|---|---|
| TCP 5432 | Standard PostgreSQL port. |
These are the environment variables understood by the container image.
| Variable | Description |
|---|---|
| POSTGRES_PASSWORD | The password for the superuser. Also see POSTGRES_USER environment variable. |
| POSTGRES_USER | Optionally specifies the name of the superuser. Defaults to postgres. |
| PGDATA | Optionally specifies the directory location of the database files. Defaults to /var/lib/postgresql/data. |
| POSTGRES_DB | Optionally specifies the name of the default database to be created when the image is first started. Defaults to the value of POSTGRES_USER. |
| POSTGRES_INITDB_ARGS | Optionally specifies arguments to send to postgres initdb. For example. --data-checksums --encoding=UTF8. |
| POSTGRES_INITDB_WALDIR | Optionally specifies a location for the Postgres transaction log. Defaults to a subdirectory of the main Postgres data folder (PGDATA). |
These are the filesystem paths used by the container image.
| Path | Description |
|---|---|
| /var/lib/postgresql/data | Stores the database files. This is the default which can altered by PGDATA environment variable. |