无法连接到在Docker上运行的PostgreSQL。

huangapple go评论75阅读模式
英文:

Unable to connect to PostgresSQL running on Docker

问题

以下是要翻译的内容:

我在Docker上运行了以下的PostgreSQL,但无法连接。当我尝试使用Visual Studio 2022扩展[Npgsql PostgreSQL Integration]1连接时,出现错误连接时发生异常。当我尝试使用VS Code扩展[PostgreSQL by Chris Kolkman]2连接时,出现错误connect ETIMEDOUT 172.17.0.2:5432。然而,当我运行连接到相同PostgreSQL并使用相同连接字符串的C# REST API时,我能够成功连接。我想要更改一些表的模式,并且我想要连接到数据库运行ALTER TABLE脚本:

Docker

C:\Users\HT>docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                           NAMES
4b22768d9f5e   postgres     "docker-entrypoint.s…"   6 days ago       Up 6 days       5432/tcp                                        gb-postgres

连接字符串

Host=172.17.0.2;Database=GB3;Username=postgres;Password=Sila.1009

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["GB3API/GB3API.csproj", "GB3API/"]
RUN dotnet restore "GB3API/GB3API.csproj"
COPY . .
WORKDIR "/src/GB3API"
RUN dotnet build "GB3API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "GB3API.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "GB3API.dll"]

迁移失败

PM> Add-Migration
cmdlet Add-Migration at command pipeline position 1
Supply values for the following parameters:
Name: PassColumnAddedInTestResult
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Failed to connect to 172.17.0.2:5432
To undo this action, use Remove-Migration.

我如何安装PostgreSQL

Step 1 - 安装Postgres

首先在命令行中运行
docker pull postgres

然后运行
docker run --name gb-postgres -e POSTGRES_PASSWORD=Sila.1009 -d postgres

Step 2 - 安装完成后,您需要找到您的实例IP地址

首先在命令行中运行
docker ps

复制容器的{id},然后运行

docker inspect {id of the container}

复制容器的IPAddress

Step 3 - 配置GB3 API

打开GB3 API中的appSettings.json

在dbConnectionString条目中,主机应该是容器的IP地址

Step 4 - 运行GB3 API容器

如果您是从Visual Studio运行API,请选择Docker配置文件并开始调试

如果您是在VS之外运行API:

从命令行进入GB3 API文件夹,确保您位于与Dockerfile相同的级别,然后运行

docker build --tag 'image_name'
docker run --detach 'image_name'
英文:

I have the following PostgreSQL running on Docker, which I cannot connect to. When I try to connect using Visual Studio 2022 extension [Npgsql PostgreSQL Integration]1, I get an error Exception while connecting. And when I try to connect using VS Code extension [PostgreSQL by Chris Kolkman]2, I get an error connect ETIMEDOUT 172.17.0.2:5432. However, when I run C# REST API that connects to the same PostgreSQL using the same connection string, I am able to connect successfully. I wanted to change schema of some tables and I wanted to connect to database to run ALTER TABLE scripts:

Docker

C:\Users\HT>docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED          STATUS          PORTS                                           NAMES
4b22768d9f5e   postgres     "docker-entrypoint.s…"   6 days ago       Up 6 days       5432/tcp                                        gb-postgres

Connection String

Host=172.17.0.2;Database=GB3;Username=postgres;Password=Sila.1009

Dockerfile


FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["GB3API/GB3API.csproj", "GB3API/"]
RUN dotnet restore "GB3API/GB3API.csproj"
COPY . .
WORKDIR "/src/GB3API"
RUN dotnet build "GB3API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "GB3API.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "GB3API.dll"]

Migration Fail

PM> Add-Migration
cmdlet Add-Migration at command pipeline position 1
Supply values for the following parameters:
Name: PassColumnAddedInTestResult
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Failed to connect to 172.17.0.2:5432
To undo this action, use Remove-Migration.

How I am installing PostgreSQL

Step 1 - Install Postgres

In the command line first
docker pull postgres

then 
docker run --name gb-postgres -e POSTGRES_PASSWORD=Sila.1009 -d postgres

Step 2 - Once Postgres is install you need to find out your instance IP address

In the command line first
docker ps

copy the {id  of the container} of the container and run

docker inspect {id  of the container}

copy the IPAddress of the container

Step 3 - Configure GB3 API

Open appSettings.json from the GB3 API

In dbConnectionString entry host should be the containers IP address

Step 4 - Run GB3 API container

if you are running the API from visual studio choose the docker profile and start debugging

if you are running the API out of VS:

from the command line go to the GB3 API folder, be sure you are at the same level of dockerfile, then

docker build --tag 'image_name'
docker run --detach 'image_name'

答案1

得分: 1

在连接字符串中使用主机作为 127.0.0.1localhost

主机=localhost;数据库=GB3;用户名=postgres;密码=Sila.1009
英文:

Use Host as 127.0.0.1 or localhost in connection string:

Host=localhost;Database=GB3;Username=postgres;Password=Sila.1009

huangapple
  • 本文由 发表于 2023年7月3日 14:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定