4

I have a .sql file from one of my production servers, and am now trying to load it into the database of another. I tried python manage.py loaddata, but its giving me an error

CommandError: Problem installing fixture 'social': sql is not a known serialization format.

How can I load my data into postgres from a .sql file with manage.py ?

TJB
  • 3,706
  • 9
  • 51
  • 102

3 Answers3

20

You can try

cat filename.sql | python manage.py dbshell
SHR
  • 7,940
  • 9
  • 38
  • 57
Leon Chew
  • 201
  • 2
  • 3
  • I don't know why this get more upvotes. This does answer the og posters question. I understand you want to use `psql` when possible to import a `.sql` file. But I recently had a situation where this was more work than doing it like @leon-chew described. `psql` was not on the same server. I either had to install it, or `rsync` the file to the database, then use `psql` to import it. Or just use this method. – Byron Mansfield Feb 19 '19 at 18:26
  • easy and smooth answer... Thanks – w411 3 Jul 15 '20 at 07:18
  • Perfect! This works as expected! – Vincent Dec 09 '21 at 16:34
  • This works like magic! I wonder why this is not the accepted answer? On windows, linux cat equivalent to read file content and concat would be type. So on windows use type filename.sql | python manage.py dbshell. You need sqlite3 executable to open an sqlite3 shell and you need mysqlclient for mysql users. Thanks so much @Leon Chew – Drizzle Jan 24 '23 at 04:24
4

Django managment doesn't work with SQL as per documentation on initial data

A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you’ve already got some data is to use the manage.py dumpdata command

Fixtures can be written as JSON, XML or YAML (with PyYAML installed) documents.

Loading data is easy: just call manage.py loaddata <fixturename>, where is the name of the fixture file you’ve created

If you still want to load data with SQL you could make data migration

iklinac
  • 14,944
  • 4
  • 28
  • 30
0

You can't import straight sql data dump using django. You should use pg's internal db import tools for that.

Jason
  • 11,263
  • 21
  • 87
  • 181