SQLite to PostgreSQL

Опубликовано: 10 Июнь 2026
на канале: Hidayat Mansuri
13
1

https://medium.com/@hidayat.mansuri/a-comp...

"Welcome! In this guide, we’re moving a Django project from the default *SQLite3* database to **PostgreSQL**, which is the industry standard for production-ready web applications. PostgreSQL offers better performance for multiple users and supports advanced data types that SQLite doesn't."

"Step one is getting PostgreSQL on your machine. You can download the installer from the official website. When you run the installer, keep the defaults, but make sure you *remember the password* you set for the `postgres` user—you’ll need it very soon."

"Once installed, open **pgAdmin 4**. Right-click 'Servers' to connect using that password, then create a new database. Let’s call ours `project_h_db`."

"Next, we need a 'bridge' so Django can talk to PostgreSQL. In your terminal, run `pip install psycopg2-binary` and then update your `requirements.txt` file."

"Now, if you have existing data like users or blog posts in SQLite, you should back it up. Use the command `python manage.py dumpdata`, but make sure to **exclude permissions and contenttypes**, as Django will recreate these automatically in the new database."

"Now for the configuration. Open your `.env` file and add your database details: the name, your `postgres` username, your password, and the host and port, which are usually `127.0.0.1` and `5432`. Then, update the `DATABASES` section in your `settings.py` to use these environment variables."

"With your settings updated, tell Django to create the new tables in PostgreSQL by running `python manage.py migrate`."

"Now, you might run into a common roadblock when trying to load your data: a `UnicodeDecodeError`. This often happens on Windows because a special character, like an **em dash**, was saved using a local encoding instead of **UTF-8**."

"To fix this, we have to convert the `data.json` file. We used a script to read the file using the original Windows encoding and rewrite it as a clean UTF-8 file. Once converted, you can successfully run `python manage.py loaddata data.json`."

"In our verification, we saw *315 objects* successfully installed into the new database. Finally, run your server and verify that everything is working smoothly. You’ve successfully migrated your project!"