Access postgres db on host machine from container built with docker compose - postgresql

I have a Postgres db set up on a ubuntu box and on the same box I have some containers from a docker compose which trying to access the database on this box. (The Postgres/database is NOT in a container. it is installed on the box)
I can access the db fine using dbeaver and can even connect to it from the site running on my dev environment on my pc
I'm using a .net core backend to connect to the db.
I have added this to the end of my backend service in the docker compose:
extra_hosts:
- "host.docker.internal:host-gateway"
Also I have tried adjusting the connection string and nothing works
Server=host.docker.internal;Port=5432;Database=mydb;User Id=xx;Password=xx;
Server=<IP address>;Port=5432;Database=mydb;User Id=xx;Password=xx;
Server=172.17.0.1;Port=5432;Database=mydb;User Id=xx;Password=xx;
Any ideas what to do?
in the logs I can see the following error
Connection id "0HMMVM72IIA14", Request id "0HMMVM72IIA14:00000002": An unhandled exception was thrown by the application.
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
---> Npgsql.NpgsqlException (0x80004005): Failed to connect to <IP REMOVED>:5432
---> System.TimeoutException: Timeout during connection attempt
at Npgsql.Internal.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)

Related

docker compose postgresql connection ExtendedSocketException: Cannot assign requested address

This is my postgresql migration code. There is no problem when i running project in visual studio.
services.AddDbContext<myDbContext>(options => options.UseNpgsql("User ID=myuser;Password=123;Host=localhost;Port=5432;Database=testDb;Pooling=true;"));
using var scope = services.BuildServiceProvider().CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<myDbContext>();
dbContext.Database.Migrate();
But when ı run in docker compose mode, i gets this error at line dbContext.Database.Migrate()
ExtendedSocketException: Cannot assign requested address [::1]:5432
This exception was originally thrown at this call stack:
System.Net.Sockets.Socket.DoConnect(System.Net.EndPoint, System.Net.Internals.SocketAddress)
System.Net.Sockets.Socket.Connect(System.Net.EndPoint)
Npgsql.NpgsqlConnector.Connect(Npgsql.Util.NpgsqlTimeout)
can you try this connection string
"User ID=myuser;Password=123;Host=host.docker.internal;Port=5432;Database=testDb;Pooling=true;"
I mean, use host.docker.internal instead of localhost

quickstart error - Using EF Migrations for local SQL Server and keep loosing db connection

I am following IdentityServer4 quickstart and trying to migrate in memory data to my local SQL Server (not SQL express or LocalDB that came with VS). My connection string is:
#"Server=localhost,1434;Database=MyIDS;user id=tester_1;Password=tester_1;trusted_connection=yes;".
When I start my IdentityServer, it creates the enpty db, MyIDS, and then throw an exception with 2 inner exceptions:
Inner Exception 1:
SqlException: A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - An established connection was aborted by the software in your host machine.)
Inner Exception 2:
Win32Exception: An established connection was aborted by the software in your host machine.
Can anyone tell me what's going on here? Why a working connection always gets dropped?
localhost,1434 looks wrong, you don't need to provide the port 1434 and the commma should not be used either, it should be a colon in that case.
I typically use for local development:
server=.;Database=ASPIdentity;Trusted_Connection=True;
dot means localhost, if you use sqlexpress the connetion string would become
server=.\\sqlexpress;Database=ASPIdentity;Trusted_Connection=True;

Can my EF application connect to PostgreSQL using user postgres?

I am writing an EF application based on https://learn.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli#create-the-database, except that I am using postgresql.
My DbContext class has
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(
#"Server=localhost;Port=5432;Database=Blogging;User Id=postgres;Password=abc;");
}
and I fail to run it:
$ dotnet run
Inserting a new blog
Unhandled exception. Npgsql.PostgresException (0x80004005): 28P01: password authentication failed for user "postgres"
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.AuthenticateMD5(String username, Byte[] salt, Boolean async)
at Npgsql.NpgsqlConnector.Authenticate(String username, NpgsqlTimeout timeout, Boolean async)
at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.ConnectorPool.AllocateLong(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnection.Open()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransaction(IsolationLevel isolationLevel)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransaction()
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
at EFGetStarted.Program.Main() in /home/ethan/mydata/academic discipline/study objects/areas/formal systems/math/categories of mathematics/meta math, or foundations of math/composition/set theory/a class of sets/operations/product space/a subset ie relation/programming/database/data models/specific-data-models/convert/add-query-engine-above-query-engine/Relational2Object/CSharp/EntityFramework/official/202010/code/EFGetStarted/Program.cs:line 14
Exception data:
Severity: FATAL
SqlState: 28P01
MessageText: password authentication failed for user "postgres"
File: auth.c
Line: 328
Routine: auth_failed
When connecting to PostgreSQL using psql instead of my application, I can run sudo -u postgres psql -U postgres and it succeeds, and on the other hand:
$ psql -U postgres
psql: FATAL: Peer authentication failed for user "postgres"
So I thought the following would work, but it still fails.
$ sudo -u postgres dotnet run
/usr/share/dotnet/sdk/3.1.100/NuGet.targets(528,5): error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/5d56c6a196882e7c1183dc543c1302123bf253ba' for operations on '/home/ethan/.nuget/NuGet/NuGet.Config'. This may mean that a different user or administator is holding this lock and that this process does not have permission to access it. If no other process is currently performing an operation on this file it may mean that an earlier NuGet process crashed and left an inaccessible lock file, in this case removing the file '/tmp/NuGetScratch/lock/5d56c6a196882e7c1183dc543c1302123bf253ba' will allow NuGet to continue.
The build failed. Fix the build errors and run again.
Why does it still fail?
Can my EF application connect to PostgreSQL using user postgres?
Is user postgres by convention to have the most privileges?
Is it a good idea to let an application to access PostgreSQL with user postgres?
There is always a superuser in a PostgreSQL database, and typically it is called postgres.
On no account you may use that user for your application. Doing so is an unnecessary and dangerous security problem. Create another user that is not a superuser.
To debug your login problem, examine pg_hba.conf and the PostgreSQL log file.

Cannot deploy war to GlassFish 4.1.2 server because database can't be found

I'm working through a book on GlassFish4 application server and have trouble deploying a sample war to the server. The server says:
"Error occurred during deployment: Exception while deploying the app [jpaintro-1.0] : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: The connection was refused because the database customerdb was not found. Error Code: 0. Please see server.log for more details."
The database server is Derby that comes with GlassFish. The database was created with this from the ./ij prompt:
connect 'jdbc:derby://localhost:1527/customerdb;create=true';
Next I could logon:
connect 'jdbc:derby://localhost:1527/customerdb;user=dev;password=dev';
and successfully enter table creation data and insert table data. Next I could retrieve this data with SQL. All fine, so the database exists.
Physical location database:
/home/fedora/GlassFish/glassfish4/glassfish/databases/1527/customerdb
The DERBY_HOME environment value was set.
I had to set up the following resources from the command line prompt:
./asadmin create-jdbc-connection-pool --datasourceclassname org.apache.derby.jdbc.ClientDataSource --restype javax.sql.DataSource --property DatabaseName=customerdb:User=dev:password=dev CustomerDBPool
./asadmin create-jdbc-resource --connectionpoolid CustomerDBPool jdbc/__CustomerDBPool
And I could verify in the GlassFish console that they were created successfully. The JDBC connection pool does indeed have an entry CustomerDBPool with an "additional property" name=DatabaseName, value=customerdb as well as user/password dev/dev. Also in the console is a JDBC resource with JNDI name jdbc/__CustomerDBPool and pool name CustomerDBPool
The war-file that I can't deploy contains a JPA-entity Customer and CDI named bean JpaDemoBean. The latter constructs a few Customer object and persists them to the database.
In the persistence.xml there is merely a jta-data-source element with value jdbc/__CustomerDBPool
I am all following literally "from the book".
Now why can't I deploy the war?
And all of a sudden I can't connect to the database via command line tool ij either:
ERROR 08004: The connection was refused because the database customerdb was not found.
I can stop and start the database server from the command line, but during startup I get the message:
"Invalid database home directory: /home/fedora/GlassFish/glassfish4/glassfish/databases"
Nevertheless the startup process continues and says it started the database successfully.
Thanks in advance!

Failed connection in MongoVUE

I'm trying to create a new connection in MongoVUE but I have an error : "Connection was refused".
The connection configuration is:
Name: database
Server: 127.0.0.1
Port: 27017(default)
Username:admin
Connection was refused Unable to connect to server 127.0.0.1:27017:
Object reference not set to an instance of an object.. Type:
MongoDB.Driver.MongoConnectionException Stack: at
MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan
timeout, ReadPreference readPreference) at
MongoDB.Driver.MongoServer.Connect(TimeSpan timeout) at
MongoDB.Driver.MongoServer.Connect() at
MangoUI.MMongo.QSDlL5xzK686iCExThO(Object ) at
MangoUI.MMongo.Open(Boolean mustWrite) at MangoUI.MMongo.Open()
at MangoUI.MConnection.get_IsValid() at
MangoUI.WinConnect.SCOjR9kYRPerNNngykW(Object ) at
MangoUI.WinConnect.btnTest_Click(Object sender, EventArgs e)
Object reference not set to an instance of an object. Type:
System.NullReferenceException Stack: at
MongoDB.Driver.MongoServerInstance.RefreshStateAsSoonAsPossible()
at MongoDB.Driver.Internal.MongoConnection.HandleException(Exception
ex) at
MongoDB.Driver.Internal.MongoConnection.SendMessage(BsonBuffer buffer,
Int32 requestId) at
MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage
message) at
MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection
connection) at
MongoDB.Driver.MongoServerInstance.RunCommandAs[TCommandResult](MongoConnection
connection, String databaseName, IMongoCommand command) at
MongoDB.Driver.MongoServerInstance.Ping(MongoConnection connection)
at MongoDB.Driver.MongoServerInstance.Connect() at
MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan
timeout, ReadPreference readPreference)
How can I fix it and create connection? Help please.
Maybe you have solved this problem.I just provide how I solve for whom come with it later:
check if your mongodb is on.
Open the browser, input the urllocalhost:27017, you should find that the web page have words like"it seems that you attend to connect the mongodb", then go to step 3.
If not go to step 2.
keep your mongodb on. you should add the install directory to the path variable.
Then create a directory to store you db,for example"D:\data\db\".
Then input this commandmongod --dbpath D:\data\db in the console, never close the console if you want to make connection.
Then go step 3.
Use MongoBooster rather than MongVUE. MongoVUE is nolooger update since 2014. However, mongobooster is well and good,support mongodb from 2.2 to 3.2
That is all.