Skip to main content

Piccolo Postgres integration for Red

Project description


Piccolo Postgres integration for Red-DiscordBot, although it could be used with any dpy bot as an easy wrapper for making postgres with cogs more modular.

Postgres 15 Red-DiscordBot

Py black license

Forks Stars Lines of code


pip install red-postgres

File structure for using with cogs

    ├── db/
    │   ├── migrations/
    │   ├──
    │   ├──
    │   ├──


Cog Usage

import asyncio
from pathlib import Path

from piccolo.engine.postgres import PostgresEngine
from redbot.core import commands
from import Red

from red_postgres import register_cog
from .db.tables import MyTable

class PiccoloTemplate(commands.Cog):
    def __init__(self, bot: Red, *args, **kwargs):
        super().__init__(*args, **kwargs) = bot
        self.db: PostgresEngine = None

    async def cog_load(self):

    async def setup(self):
        config = await"piccolo")

        # Pass the root directory of the cog
        cog_root = Path(__file__).parent

        self.db = await register_cog(cog_root, config, [MyTable])

    async def cog_unload(self):
        if self.db:
            await self.db.close_connection_pool()

The shared api token config for piccolo should be the following:

  "database": "database_name",
  "host": "host ip (",
  "port": "port (5432)",
  "user": "user123",
  "password": "password123"

Note: database name in your config should normally be the default "postgres", this library will automatically handle connecting your cogs to their own database

The register method connects to the database specified in config, creates the a new database with the name of the registering cog, registers any tables, runs any migrations, sets the new engine object to all tables, and returns the raw engine object.

  • The name of the database will be the the name of the cog's folder, not the name of the main file

You can then use your piccolo table methods like so:

count = await MyTable.count()
objects = await MyTable.objects().where(MyTable.text == "Hello World")

The engine associated with your tables after registering the cog is connected to the database named the same as the cog that registered them, thus using this integration with multiple cogs will not interfere, as each cog will create its own database.

  • If your cog's folder name is MyCog then the database will be named my_cog

Piccolo Configuration Files

Your piccolo configuration files must be setup like so. This is really only used for migrations.

  • When migrations are run, the os environment variables are mocked in subprocess, so there should be no conflicts

import os

from piccolo.conf.apps import AppRegistry
from piccolo.engine.postgres import PostgresEngine

DB = PostgresEngine(
        "database": os.environ.get("POSTGRES_DATABASE"),
        "user": os.environ.get("POSTGRES_USER"),
        "password": os.environ.get("POSTGRES_PASSWORD"),
        "host": os.environ.get("POSTGRES_HOST"),
        "port": os.environ.get("POSTGRES_PORT"),

APP_REGISTRY = AppRegistry(apps=["db.piccolo_app"])

import os

from piccolo.conf.apps import AppConfig, table_finder

CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))

APP_CONFIG = AppConfig(
    migrations_folder_path=os.path.join(CURRENT_DIRECTORY, "migrations"),

for table_classes add in the list of tables you're using

Local development and making migrations

Handing migrations is up to you, but one way to do it is to make migrations locally like so:

import os
import subprocess
from pathlib import Path

root = Path(__file__).parent
env = os.environ.copy()
env["PICCOLO_CONF"] = "db.piccolo_conf"
env["POSTGRES_HOST"] = "localhost"
env["POSTGRES_PORT"] = "5432"
env["POSTGRES_USER"] = "user"
env["POSTGRES_PASSWORD"] = "password123!"
env["POSTGRES_DATABASE"] = "templatecog"
env["PYTHONIOENCODING"] = "utf-8"

def make():
    migration_result =
        ["piccolo", "migrations", "new",, "--auto"],

def run():
    run_result =
        ["piccolo", "migrations", "forward",],

if __name__ == "__main__":

You would have a similar file in the root of each of your cog folders, here you would create the migrations to include in your cog folder for users to run when they load up the cog.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

red-postgres-0.3.2.tar.gz (7.4 kB view hashes)

Uploaded source

Built Distribution

red_postgres-0.3.2-py3-none-any.whl (7.6 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page