Skip to main content

A PostgreSQL library for Flask inspired by Flask-SQLAlchemy. This library provides an easy-to-use interface for interacting with PostgreSQL databases in Flask applications.

Project description

Flask PostgreSQL Library

The Flask PostgreSQL library provides a convenient interface for integrating PostgreSQL databases into Flask applications. This library simplifies database interactions by offering an easy-to-use API similar to Flask-SQLAlchemy.

Installation

You can install the Flask PostgreSQL library using pip:

pip install flask-pgsql --user

Usage

Works on existing code

from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'database\\database.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app) # Creating an SQLAlchemy instance
...
if __name__ == "__main__":
    if RESET:
        with app.app_context(): db.create_all()

replace it by

from flask_postgresql import PostgreSQL
db = PostgreSQL(hostname=hostname, port=port, database=database, username=username, password=password)
...
if __name__ == "__main__":
    if RESET:
        db.create_all() # with app.app_context(): db.create_all() will also work

array support

class Test(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    data = db.Column(db.Integer, array=True)

    def __repr__(self):
        return f"Test({self.id}, {self.data})"
db.create_all()
p = Test(data = [21, 24])
db.session.add(p)
db.session.commit()
Test.query.get(id=1).data #-> [21, 24]    

Initializing the Database Connection

To initialize the PostgreSQL connection, import the PostgreSQL class from flask_postgresql and provide the necessary connection parameters:

import os
from flask_postgresql import PostgreSQL

# Retrieve database connection parameters from environment variables
hostname = os.getenv("db_hostname")
port = int(os.getenv("db_port"))
database = os.getenv("db_database")
username = os.getenv("db_username")
password = os.getenv("db_password")

# Initialize the PostgreSQL connection
db = PostgreSQL(hostname=hostname, port=port, database=database, username=username, password=password)

Defining Models

Define your database models by subclassing db.Model. Here's an example of defining BLOGS and USERS models:

class BLOGS(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, nullable=False)
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.String(200), nullable=True)
    data = db.Column(db.Text, nullable=False)

    def __repr__(self):
        return f"{self.id}). Name : {self.user_id}, title: {self.title}, description: {self.description}, data: {self.data}"

class USERS(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    age = db.Column(db.Integer)
    is_active = db.Column(db.Boolean, default=True)
    bio = db.Column(db.Text)
    details = db.Column(db.JSON, nullable=True)
    profile_image = db.Column(db.LargeBinary)
    created_at = db.Column(db.DateTime, default=db.func.now())

    def __repr__(self):
        return f"Test({self.id}, {self.username}, {self.email}, {self.age}, {self.is_active}, {self.bio}, {self.profile_image}, {self.created_at})"

Creating Tables

Create database tables using the create_all() method:

db.create_all()
# or you can recreate any special table
# USERS.create()
# BLOGS.create()

Querying Data

You can query data using the query attribute of your models:

users = USERS.query.all()
user = USERS.query.get(id=12)

Adding Data

You can add data to the database using the add() method:

new_user = USERS(username="example_user")
db.session.add(new_user)
db.session.commit()

Deleting Data

You can delete data from the database using the delete() method:

user_to_delete = USERS.query.get(id)
user_to_delete.delete()
db.session.commit()

Compatibility

While the Flask PostgreSQL library is designed for Flask applications, it can also be used in other frameworks or standalone Python scripts that require PostgreSQL database integration.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

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

flask-pgsql-0.0.7.tar.gz (5.6 kB view details)

Uploaded Source

File details

Details for the file flask-pgsql-0.0.7.tar.gz.

File metadata

  • Download URL: flask-pgsql-0.0.7.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for flask-pgsql-0.0.7.tar.gz
Algorithm Hash digest
SHA256 bdece099c3c5a8fab7c1508809a29481d2c516fe2ed4a734ae5491870cb96a84
MD5 a0b1231ed3eeb50119a9fdabafd81696
BLAKE2b-256 ee4af480885c14e9fd04481a2035a109a023cd6627cfbe7d09ab07b3754185fc

See more details on using hashes here.

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