PostgreSQL¶
References¶
About¶
PostgreSQL is an SQL server, for those that need an SQL database.
The database is available on port 5432
Configuration¶
The service definition includes the following environment variables:
TZ
your timezone. Defaults toEtc/UTC
POSTGRES_USER
. Initial username. Defaults topostuser
.POSTGRES_PASSWORD
. Initial password associated with initial username. Defaults toIOtSt4ckpostgresDbPw
(postpassword
for old menu).POSTGRES_DB
. Initial database. Defaults topostdb
.
You can either edit the environment variables directly or provide your own substitutes by editing ~/IOTstack/.env
. Example:
$ cat ~/IOTstack/.env
TZ=Australia/Sydney
POSTGRES_PASSWORD=oneTwoThree
When the container is brought up:
TZ
will have the valueAustralia/Sydney
(from.env
)POSTGRES_PASSWORD
will have the valueoneTwoThree
(from.env
)POSTGRES_USER
will have the valuepostuser
(the default); andPOSTGRES_DB
will have the valuepostdb
(the default).
The TZ
variable takes effect every time the container is brought up. The other environment variables only work the first time the container is brought up.
It is highly recommended to select your own password before you launch the container for the first time. See also Getting a clean slate.
Management¶
You can interact with the PostgreSQL Relational Database Management System running in the container via its psql
command. You can invoke psql
like this:
$ docker exec -it postgres bash -c 'PGPASSWORD=$POSTGRES_PASSWORD psql $POSTGRES_DB $POSTGRES_USER'
Because of the single quotes (') surrounding everything after the
-c
, expansion of the environment variables is deferred until the command is executed inside the container.
You can use any of the following methods to exit psql
:
- Type "\q" and press return
- Type "exit" and press return
- Press control+D
password change¶
Once you have logged into psql
you can reset the password like this:
# ALTER USER «user» WITH PASSWORD '«password»';
Replace:
«user»
with the username (eg the default username ispostuser
)«password»
with your new password.
Notes:
- Changing the password via the
ALTER
command does not update the value of thePOSTGRES_PASSWORD
environment variable. You need to do that by hand. -
Whenever you make a change to a running container's environment variables, the changes will not take effect until you re-create the container by running:
$ cd ~/IOTstack $ docker-compose up -d postgresql
Getting a clean slate¶
If you need to start over, proceed like this:
$ cd ~/IOTstack
$ docker-compose down postgres
$ sudo rm -rf ./volumes/postgres
$ docker-compose up -d postgres
see also if downing a container doesn't work