You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行python manage.py makemigrations时出现[WinError 126]错误求助

Troubleshooting WinError 126 When Running 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 (replace 2.x with 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.gis to INSTALLED_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) or libpq.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:

  1. Uninstall PostgreSQL and PostGIS completely, including deleting their installation directories and removing leftover entries from system PATH.
  2. Install PostgreSQL 9.6 first, using the default installation path (avoid spaces or non-ASCII characters in the path—these can break DLL loading).
  3. Run the OSGeo PostGIS installer for pg96, making sure it detects your PostgreSQL 9.6 installation correctly during setup.

内容的提问来源于stack exchange,提问作者EleanorShellstrop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:24:03