执行python manage.py makemigrations时出现[WinError 126]错误求助
python manage.py makemigrations with GeoDjango & PostGIS Hey there, let's work through this WinError 126 issue—it almost always boils down to missing system dependencies, misconfigured paths, or mismatched versions between PostgreSQL and PostGIS. Here's a step-by-step breakdown to diagnose and fix it:
1. Verify Version Compatibility
First, double-check that your PostGIS installation matches your PostgreSQL version. You downloaded the pg96 PostGIS package, so make sure you're running PostgreSQL 9.6 exactly. Mismatched versions (e.g., using PostGIS for pg96 with PostgreSQL 10) will cause DLL loading failures every time.
2. Check System PATH Environment Variables
WinError 126 often means Windows can't find required DLL files. Add these directories to your system PATH (adjust paths to match your actual installation):
- PostgreSQL's bin directory:
C:\Program Files\PostgreSQL\9.6\bin - PostGIS's bin directory (usually nested under PostgreSQL's bin):
C:\Program Files\PostgreSQL\9.6\bin\postgis-2.x(replace2.xwith your installed PostGIS version) - Also ensure GDAL and PROJ directories (if installed separately) are in the PATH—these are critical dependencies for PostGIS.
After updating PATH, restart your command prompt/IDE to apply the changes.
3. Validate PostGIS Installation in Your Database
Open pgAdmin, connect to your Django database, and run this SQL command:
SELECT postgis_version();
- If it returns a version string (e.g.,
2.3.4 r15246), PostGIS is properly enabled at the database level. - If it throws an error, you need to enable the PostGIS extension first:
CREATE EXTENSION postgis;
4. Confirm Django Settings Configuration
Make sure your settings.py is set up correctly for GeoDjango:
- Add
django.contrib.gistoINSTALLED_APPS:INSTALLED_APPS = [ # ... other existing apps 'django.contrib.gis', ] - Use the PostGIS database engine instead of the standard PostgreSQL backend:
DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': 'your_db_name', 'USER': 'your_db_user', 'PASSWORD': 'your_db_password', 'HOST': 'localhost', 'PORT': '5432', } }
5. Diagnose Missing DLL Dependencies
If the above steps don't work, use a tool like Dependency Walker to check for missing DLLs:
- Download Dependency Walker and open
postgis-2.x.dll(located in your PostGIS bin directory) orlibpq.dll(in PostgreSQL's bin directory). - The tool will highlight any missing dependent DLLs—these are the files you need to add to your PATH or reinstall.
6. Clean Reinstallation (If All Else Fails)
Sometimes a fresh install fixes lingering issues:
- Uninstall PostgreSQL and PostGIS completely, including deleting their installation directories and removing leftover entries from system PATH.
- Install PostgreSQL 9.6 first, using the default installation path (avoid spaces or non-ASCII characters in the path—these can break DLL loading).
- Run the OSGeo PostGIS installer for pg96, making sure it detects your PostgreSQL 9.6 installation correctly during setup.
内容的提问来源于stack exchange,提问作者EleanorShellstrop

