TimeScaleDB from Docker

TimeScaleDB is a powerful and scalable time-series database that can handle large amounts of data and complex queries with ease. If you want to try TimeScaleDB, one way to get started is by running it in a Docker container. Docker allows you to easily create, deploy, and run applications in a containerized environment, which can be useful for testing and development purposes.

In this section, we’ll show you how to configure a TimeScaleDB database from a Docker container. We’ll cover the basic steps for setting up a container, installing TimeScaleDB, and connecting to the database from outside the container. By the end of this section, you’ll have a working TimeScaleDB database running in a Docker container, ready for you to experiment with and build upon.

Get Docker TimeScaleDB image

To get started, we first need to download the TimeScaleDB Docker image. Open up a terminal and enter the following command:

[ ]:
docker pull timescale/timescaledb:latest-pg15

This will download the latest TimeScaleDB image for PostgreSQL 15. If you want to use a different version of PostgreSQL, replace pg15 with the version you need.

TimescaleDB container

To run the TimeScaleDB container, use the following command:

[ ]:
docker run -d --name timescaledb \
-p 127.0.0.1:5432:5432 \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=dunderlab_timescale_v1 \
timescale/timescaledb:latest-pg15

The command consists of several options:

  • docker run: Runs a new container.

  • -d: Detaches the container and runs it in the background.

  • --name timescaledb: Assigns the name “timescaledb” to the container.

  • -p 127.0.0.1:5432:5432: Maps the local port 5432 to the container’s port 5432, allowing access to the database from outside the container.

  • -e POSTGRES_PASSWORD=password: Sets the environment variable POSTGRES_PASSWORD to “password”, which is the password for the default user “postgres”.

  • -e POSTGRES_DB=dunderlab_timescale_v1: Sets the environment variable POSTGRES_DB to “dunderlab_timescale_v1”, which is the name of the database that will be created inside the container. timescale/timescaledb:latest-pg15: Specifies the Docker image to use for the container, which in this case is “timescale/timescaledb” with the “latest-pg15” tag, indicating the latest version of TimeScaleDB built on PostgreSQL 15.

Overall, this command creates and starts a new container running the latest version of TimeScaleDB built on PostgreSQL 15, with the database name “dunderlab_timescale_v1” and the password “password” for the default user. The container is named “timescaledb” and can be accessed on port 5432 of the local machine.

Custom TimeScaleDB container creator

This script provides a simple and convenient way to deploy a TimeScaleDB instance using Docker from the command line.
The script accepts command-line arguments for container name, Docker network, database password, and port. These arguments are optional with default values provided.

To use the script, execute it from the command line with optional arguments as shown in the example below:

timescaledbapp_create --name mytimescaledb --network mynetwork --password mypassword --port 5433

This would launch a Docker container with the following configuration:

  • Container name: mytimescaledb

  • Docker network: mynetwork

  • Database password: mypassword

  • Port: 5433

By default, if you do not provide these arguments, the script will use the following values:

  • Container name: timescaledb

  • Docker network: mynetwork

  • Database password: password

  • Port: 5432

Please note, you will need Docker installed and running on your machine to utilize this script.

Access to database

This is a Docker command used to access the TimeScaleDB database inside the container.

[ ]:
docker exec -it timescaledb psql -U postgres -d mytimescaledb

The command consists of several options:

  • docker exec: Runs a command inside a running container.

  • -it: Attaches the terminal to the container.

  • timescaledb: Specifies the name of the container to access.

  • psql -U postgres -d mytimescaledb: Specifies the command to run inside the container, which is to start the PostgreSQL command line interface (CLI) with the user “postgres” and the database “dunderlab_timescale_v1”.

Overall, this command opens a connection to the running TimeScaleDB container and starts the PostgreSQL CLI for the “mytimescaledb” database with the “postgres” user.

Configure test database

[ ]:
timescaledbapp_create --name timescaledbapp_v2
python manage.py migrate timescaledbapp --database='timescaledb'                                                                                      ─╯