如何配置Docker容器中的REST API连接Windows主机的PostgreSQL?
Here's a step-by-step guide to get your Docker container talking to the PostgreSQL instance on your Windows host:
1. Configure PostgreSQL on Windows to Allow Remote Connections
By default, PostgreSQL restricts connections to localhost. You'll need to update two config files to open it up to your Docker container:
Modify
postgresql.conf:
Navigate to your PostgreSQL data directory (typicallyC:\Program Files\PostgreSQL\<version>\data), openpostgresql.confin a text editor, and change:listen_addresses = 'localhost'to:
listen_addresses = '*'This lets PostgreSQL listen for connections on all network interfaces.
Update
pg_hba.conf:
Still in the data directory, openpg_hba.confand add a line at the end to allow connections from your Docker network:host all all 172.17.0.0/16 md5The
172.17.0.0/16is the default subnet for Docker's bridge network. If you're using a custom Docker network, replace this with your subnet. For testing purposes, you can use0.0.0.0/0to allow connections from any address (not recommended for production).Restart PostgreSQL:
Open the Windows Services manager (services.msc), find your PostgreSQL service (named something likepostgresql-x64-<version>), right-click it and select Restart.
Note: Ensure your PostgreSQL user has login privileges and access to the target database. You can set this in psql with:
ALTER USER your_db_user WITH LOGIN; GRANT ALL PRIVILEGES ON DATABASE your_db_name TO your_db_user;
2. Find the Windows Host IP Accessible from Docker
Your Docker container needs the correct IP address to reach the Windows host. The easiest method is using Docker's built-in DNS name:
Use
host.docker.internal:
Docker for Windows provideshost.docker.internalas a special DNS name that resolves to your host machine's IP. This works for both WSL2 and Hyper-V setups.Fallback to actual host IP:
Ifhost.docker.internaldoesn't work, runipconfigin a Windows command prompt. Look for the IP address associated with your active network adapter (e.g., "Ethernet adapter Ethernet" for Hyper-V, or "Ethernet adapter vEthernet (WSL)" for WSL2). Use this IP in your Django settings.
3. Update Django Database Settings
In your Django project's settings.py, modify the DATABASES configuration to point to your Windows-hosted PostgreSQL:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': '<your_database_name>', 'USER': '<postgresql_username>', 'PASSWORD': '<postgresql_password>', 'HOST': 'host.docker.internal', # Or your actual host IP 'PORT': '5432', # Default PostgreSQL port } }
Ensure psycopg2-binary is listed in your awf/requirements.txt (it's required for Django to connect to PostgreSQL in Python 2.7). If not, add it:
psycopg2-binary>=2.8.6
4. Allow PostgreSQL Connections Through Windows Firewall
Windows Firewall will block incoming connections to port 5432 by default. To open it:
- Open Windows Defender Firewall with Advanced Settings
- Click Inbound Rules > New Rule
- Select Port > Next
- Choose TCP, enter
5432as the port > Next - Select Allow the connection > Next
- Check the network types you want (e.g., Private) > Next
- Name the rule (e.g., "PostgreSQL Docker Access") > Finish
5. Test the Connection
Rebuild your Docker image to apply the settings changes:
docker build -t your-api-image .
Run the container as usual, then check the logs for any connection errors:
docker logs <container_name>
If you see errors like "could not connect to server", double-check:
- PostgreSQL is running and restarted after config changes
- The firewall rule is active
- The host IP in
settings.pyis correct - The PostgreSQL user has permissions to connect from the container's IP
内容的提问来源于stack exchange,提问作者revy




