This quick guide will spin up a Postgres 12 container accessible from localhost:9432
. This is NOT a durable setup and data is very likely to be lost after the database is shutdown. For demo purposes only.
- Docker
In one shell, run
$ docker run -e POSTGRES_PASSWORD=postgres --publish 9432:5432 --name demo-pg12 postgres:12
... Postgres logs ...
This will start Postgres 12 accessible from localhost:9432
. Username, password, and default database all are postgres
.
Then, in another shell, run
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5fbf767ba025 postgres:12 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 0.0.0.0:9432->5432/tcp demo-pg12
$ docker exec -it demo-pg12 psql -U postgres
psql (12.4 (Debian 12.4-1.pgdg100+1))
Type "help" for help.
postgres=# select 'tada! we have access to postgres!' as helloworld;
helloworld
-----------------------------------
tada! we have access to postgres!
(1 row)
Alternatively, you can connect to Postgres via a natively installed psql
client
$ PGPASSWORD=postgres psql -h localhost -p 9432 -U postgres -d postgres
psql (12.4 (Debian 12.4-1.pgdg100+1))
Type "help" for help.
postgres=#
To clean up, just <CTRL-C>
to close the first shell with Docker running Postgres. You can exit psql
with either or by typing in \q<ENTER>
.