Package that helps you to work with postgresql

postgresql, python, database, lib, library, orm, orm-library, orm-model, pg, pgsql, postgres, postgres-database, postgresql-database, python3
pip install pgpython==0.8



Use the package manager pip to install pgpython.

pip install pgpython


Connect to database

To connect to the database you must create .env file in your directory with needed variables. Example:


And that's all you need to do!

Working with tables

Next we are going to deal with tables. Imagine that you have got a table called users with next columns:

  1. id (integer)
  2. username (text)

First, you have to import pgpython BaseModel to inherit your classes from it. Code:

from pgpython import BaseModel

Next, you must import fields that you will use in your code. Fields must match up with you column type. Example:

from pgpython.fields import IntegerField, StringField

There are 4 types in total: IntegerField, DateField, JSONField, StringField

Next, you must create a class called as your table name with attributes called as your columns. Code:

class users(BaseModel):
  id = IntegerField()
  username = StringField()

That's is all you need to tie up your class and table.


Insert data

After creating a class you must create an object of this class. Using our example of users table let's code:

user = users()

Next, let's set our data to an object.

IMPORTANT If you have an auto_incremented id than you must not to set id to your object. = 1
user.username = 'c0nder'

Then, use method add()


That's all!

Selecting data from table

At first, you must, as always, create an object and set data that you need to use to select data from table. Than use method select() to select data.

user = users()
user.username = 'c0nder'
data = user.selectBy({'username': user.username})

Load object by id

user = users.loadById(1)

Updating data

user = users.loadById(1)
user.username = 'New_username'

Delete data

user = users.loadById(1)

Getting columns of table object

user = users.loadById(1)
columns = user.getColumns()

for col, obj in columns.items():
	print(col, ":", obj.value)

Printing value of object attribute

user = users.loadById(1)

Working with Schemas

To work with schemas you need to import BaseSchema.

from pgpython import BaseModel, BaseSchema

Next, we will use our table users.

class users(BaseModel):
  id = IntegerField()
  username = StringField()

Than, you need to create schema class.

class schema_name(BaseSchema):
  users = users()

Example of using:

user = schema_name.users.loadById(1)

That's all!

Added ReadMe