Python

Simple working with SQL database with SQLModel

Another great library from the creator of FastAPI.

Donald Le
2 min readSep 22, 2021

--

Providing Country: Netherlands. Public Domain

SQLModel is based on pydantic and SQLAlchemy. The library provides great support in code editors like Visual Studio Code and Pycharm. Let’s go with some simple queries, orms and create data into Postgresql database with SQLModel.

First we create a new user for Postgres database

create user testuser with encrypted password 'testuser';

Then we will create a new database in Postgres name testdb

CREATE DATABASE testdb OWNER testuser;

We will create a new virtual environment for Python, then install sqlmodel library

pip install sqlmodel

If you’re using Ubuntu like me, you would also need to install psycopg2-binary:

pip install psycopg2-binary

Ok, so let’s create an example for adding new data to our testdb

--

--

Donald Le

A passionate automation engineer who strongly believes in “A man can do anything he wants if he puts in the work”.