Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to use Sqlite instead Posgresql #1132

Answered by bwithai
auh83 asked this question in Questions
Discussion options

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched in the documentation/README.
  • I already searched in Google "How to do X" and didn't find any information.
  • I already read and followed all the tutorial in the docs/README and didn't find an answer.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

@computed_field # type: ignore[misc]
 @property
 def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
 return MultiHostUrl.build(
 scheme="postgresql+psycopg",
 username=self.POSTGRES_USER,
 password=self.POSTGRES_PASSWORD,
 host=self.POSTGRES_SERVER,
 port=self.POSTGRES_PORT,
 path=self.POSTGRES_DB,
 )

Description

I want to use sqlite instead Pstgresql, can you help me to replace it in the backend.

Operating System

Windows

Operating System Details

windows 10

Python Version

python 3.11

Additional Context

No response

You must be logged in to vote

Hi @auh83
Edit SQLALCHEMY_DATABASE_URI with this code

SQLITE_DB_FILE: str = ""
@computed_field # type: ignore[prop-decorator]
@property
def SQLALCHEMY_DATABASE_URI(self) -> str | PostgresDsn:
 if self.SQLITE_DB_FILE:
 return f"sqlite:///{self.SQLITE_DB_FILE}"
 else:
 return MultiHostUrl.build(
 scheme="postgresql+psycopg",
 username=self.POSTGRES_USER,
 password=self.POSTGRES_PASSWORD,
 host=self.POSTGRES_SERVER,
 port=self.POSTGRES_PORT,
 path=self.POSTGRES_DB,
 )

and in your .env file SQLITE_DB_FILE=my_db.sqlite --> python backend\app\initial_data.py

Replies: 2 comments 1 reply

Comment options

@tiangolo I imagine there is a simple way to remplace Postgresql by sqlite !

You must be logged in to vote
1 reply
Comment options

If the creator of FastAPI says so, then there's hope ;-)

Comment options

Hi @auh83
Edit SQLALCHEMY_DATABASE_URI with this code

SQLITE_DB_FILE: str = ""
@computed_field # type: ignore[prop-decorator]
@property
def SQLALCHEMY_DATABASE_URI(self) -> str | PostgresDsn:
 if self.SQLITE_DB_FILE:
 return f"sqlite:///{self.SQLITE_DB_FILE}"
 else:
 return MultiHostUrl.build(
 scheme="postgresql+psycopg",
 username=self.POSTGRES_USER,
 password=self.POSTGRES_PASSWORD,
 host=self.POSTGRES_SERVER,
 port=self.POSTGRES_PORT,
 path=self.POSTGRES_DB,
 )

and in your .env file SQLITE_DB_FILE=my_db.sqlite --> python backend\app\initial_data.py

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested

AltStyle によって変換されたページ (->オリジナル) /