0
votes

I have simple ASP.NET MVC 4 application (just created it), that connects to remote PostgreSQL server via NpSQL + EF client. But it tries to connect to database "postgres" which is not allowed:

Npgsql.NpgsqlException: FATAL: 28000: no pg_hba.conf entry for host "myipaddress", user "myuser", database "postgres", SSL on

Other webapp works with the same Web.config without any problems. Only difference I see are different versions of Npgsql and Npgsql.EntityFramework libraries. Working has 2.1.3, faulty one 2.2.1. But it doesn't work, when I downgrade them.

It works on my development machine which has no ph_hba.conf modifications (localhost has access to any user and any database).

Web server is running on Windows Server 2012 R2 + IIS8.5

Why is one app trying to access database "postgres" and other not? How to fix it?

Thanks for any help.

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Server=myserver;port=1234;Database=mydatabase;User Id=myuser;Password=xxx;SearchPath=dbo;SSL=True" providerName="Npgsql" />
    <add name="DefaultConnection_DatabasePublish" connectionString="Server=myserver;port=1234;Database=mydatabase;User Id=myuser;Password=xxx;SearchPath=dbo;SSL=True" providerName="Npgsql" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <httpRuntime targetFramework="4.5" />
    <compilation debug="true" targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <customErrors mode="Off" />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <contexts>
      <context type="DBConnectionTester.Models.UsersContext, DBConnectionTester">
        <!--<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[MvcSlap.Models.UsersContext, MvcSlap], [MvcSlap.Migrations.Configuration, MvcSlap]], EntityFramework, PublicKeyToken=b77a5c561934e089">
          <parameters>
            <parameter value="DefaultConnection_DatabasePublish" />
          </parameters>
        </databaseInitializer>-->
      </context>
    </contexts>
    <providers>
      <!--<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />-->
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" />
    </providers>
  </entityFramework>

</configuration>
2
sounds like the miss-config in your PostgreSQL server - Vivek S.
do've any entry in pg_hba.conf in PostgreSQL server ??? yes there should be entries .. and you've to give the host addr(ex.ip) of the system from that your trying to access PostgreSQL server - Vivek S.

2 Answers

0
votes

It was problem with migrations, which don't work with NPgSQL 2.1. I had them disabled in other webapp, but for some reason, which I don't know, they were active in this app and causing problems. Only difference between local and server database was table __MigrationHistory present on server, which I have renamed to some other name and it works now.

0
votes

Apologies but I am not allowed to comment.

My problem is at least very similar to OP but I dont understand the solution. I am getting the same NpgsqlException where the database name is "postgres" but should be what I specified in the connectionstring.

Setup

  1. My web server setup is the same, i.e. Windows Server 2012 and IIS 8.5.
  2. The Postgresql database is on its own machine.
  3. Developer machine and web server are currently using the same database which is defined in 2)
    • Developer machine connects correctly
    • Web server gets the NpgsqlException in the OP
  4. Using msbuild.exe to create a package and msdeploy.exe to deploy the package to the web server

Edit

Turns out, allowing access to database "postgres" in the pg_hba.conf file solved the problem for me.