Image Overview: postgres

Overview: postgres Chainguard Image

Minimal PostgreSQL image.

Download this Image

The image is available on cgr.dev:

docker pull cgr.dev/chainguard/postgres:latest

Usage

The only mandatory environment variable needed by the PostgreSQL Image is POSTGRES_PASSWORD.

You can test the PostgreSQL Image by running the following command:

docker run --rm -e POSTGRES_PASSWORD=password -ti --name postgres-test cgr.dev/chainguard/postgres:latest

This command will run the Image, but no data within the PostgreSQL database will persist after the Image stops running.

To persist PostgreSQL data you can mount a volume mapped to the data folder:

docker run --rm -v $PWD/data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=password -ti --name postgres-test cgr.dev/chainguard/postgres:latest

In a new terminal, exec into the running container:

docker exec -ti postgres-test bash

Connect using the postgres user:

su postgres

Use the createdb wrapper to create a test database:

createdb test

Then use the PostgreSQL client to Connect to the new database:

psql test

From there you can interact with the database as you would with any other PostgreSQL database. For instance, you can create a sample table:

CREATE TABLE accounts (
	user_id serial PRIMARY KEY,
	username VARCHAR ( 50 ) UNIQUE NOT NULL,
	password VARCHAR ( 50 ) NOT NULL,
	email VARCHAR ( 255 ) UNIQUE NOT NULL,
	created_on TIMESTAMP NOT NULL,
	last_login TIMESTAMP
);

You could then insert data into the table:

INSERT INTO accounts (username, password, email, created_on, last_login)
VALUES (
'Inky',
'p@$$w0rD',
'inky@example.com',
'2017-07-23',
'2017-07-23'
);

You can also use all of PostgreSQL’s internal meta-commands. For example, \dt will list all the tables stored within the database:

\dt
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | accounts | table | postgres
(1 row)

Further Reading

Please refer to our guide on getting started with the PostgreSQL Chainguard Image for an in-depth walk-through of how you can use the PostgreSQL Image in practice. This getting started guide outlines how to set up and run a PHP application that stores its data in a PostgreSQL database running within a containerized environment.

Last updated: 2024-04-11 12:38