Skip to main content

Python

Relyt supports the PostgreSQL connection protocol, which means any tools or software that can connect to PostgreSQL can also be used for Relyt connections.

This guide will walk you through the process of connecting to Relyt using Python.

1. Prepare the environment

Install the Python library. The following code uses psycopg2 as an example.

pip install psycopg2

2. Create a Python program

  1. Create a .py file:

    vim relyt_demo.py
  2. Write the program code in the file:

    import psycopg2
    from psycopg2 import sql

    host = "<endpoint_domain>"
    port = <endpoint_port>
    user = "<dw_user>"
    password = "<dw_user_password>"
    dbname = "<database>"

    # Connect to your Relyt database
    conn = psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)

    # Open a cursor to perform database operations
    cur = conn.cursor()

    # Drop table items if it exists and create a new one named items
    cur.execute("DROP TABLE IF EXISTS items;")
    cur.execute("CREATE TABLE IF NOT EXISTS items (id INTEGER, name VARCHAR);")

    # Insert data into table items
    cur.execute("INSERT INTO items (id, name) VALUES (1, 'item1'), (2, 'item2');")

    # Query data from items and obtain data as Python objects
    cur.execute("SELECT * FROM items;")
    rows = cur.fetchall()

    for row in rows:
    print(row)

    # Disconnect from the Relyt database
    cur.close()
    conn.close()

    The following table describes the related parameters. Specify them based on your actual conditions.

    ParameterDescription
    hostThe public endpoint for connecting to the Relyt DW service unit.
    portThe port for connecting to the Relyt DW service unit. The default port is 5432.
    userThe name of the DW user that connects to the Relyt DW service unit.
    passwordThe password of the DW user.
    dbnameThe name of the Relyt database to connect.

    The SQL commands provided in the example perform the following operations in sequence:

    • Drop a table named items if it exists

    • Create a table named items

    • Insert data to table items

    • Query data from items

    They are for reference only. You can replace them with any SQL commands you want to run.

3. Run the program

Run the program:

python relyt_demo.py