PostgreSQL: connect directly to the configured database when told not to create it - #562
Closed
DutchmanNL wants to merge 2 commits into
Closed
DutchmanNL wants to merge 2 commits into
DutchmanNL wants to merge 2 commits into
Conversation
…to create it The two-phase PostgreSQL connect always opened the maintenance database "postgres" first. With "do not create database" set it connected there, did nothing, disconnected and reconnected - so the option avoided the CREATE DATABASE, but not the connection that needs privileges nobody grants on a managed PostgreSQL. A role without CONNECT on "postgres" therefore never reached its own database at all: the first phase was refused with 42501 and the adapter looped on the 30 s reconnect forever (ioBroker#404). That is also what makes the Supabase request in ioBroker#481 fail on the normal postgresql dbtype. With the option set, connect() now skips the maintenance phase entirely. The default path is unchanged. Table creation is unaffected either way: init() only ever returns CREATE TABLE statements and runs against the configured database, tolerating tables that already exist. The admin "Test connection" button had the same hardcoded database, which is the actual defect behind ioBroker#285 - it reported a failure for a perfectly good configuration. It now tests the configured database when the option is set. Adds test/testPostgreSQLNoCreateDb.js, which builds the situation it guards: a role with no CONNECT on "postgres" and a pre-existing database of its own. It asserts the adapter connects, creates its six tables in the configured database and round-trips a value. On the unfixed code it fails with "permission denied for database postgres" and the adapter never connects. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
|
Reimplemented: #568 |
Contributor
|
Merged via #568, which carries your commit with authorship intact ( Thank you — the diagnosis was precise, and the part that turned out to matter most was one you found rather than the one in the title: the admin "Test connection" button had Two things happened on the way, for the record:
Closing in favour of #568. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #404 and #285, and is what makes the Supabase request in #481 work on the normal
postgresqldbtype.The bug
The two-phase PostgreSQL connect always opens the maintenance database
postgresfirst. With "do not create database" set, it connected there, did nothing, disconnected and reconnected:So the option avoided the
CREATE DATABASE, but not the connection that needs a privilege nobody grants on a managed PostgreSQL.A role without
CONNECTonpostgrestherefore never reached its own database at all — the first phase was refused and the adapter looped on the 30 s reconnect forever:The fix
With the option set,
connect()skips the maintenance phase entirely and goes straight to the configured database. The default path is untouched.Table creation is unaffected either way:
init()only ever returnsCREATE TABLEstatements, runs against the configured database, and tolerates tables that already exist — so the option means "do not create the database", exactly as it says.The admin "Test connection" button had the same hardcoded database, which is the actual defect behind #285: it tested
postgresrather than the configured database, and so reported a failure for a perfectly good configuration. It now tests the configured database when the option is set.Test
test/testPostgreSQLNoCreateDb.jsbuilds the situation it guards, rather than assuming it: as superuser it creates a role with noCONNECTonpostgresplus a pre-existing database of its own, then runs the adapter as that role with the option enabled. It asserts the adapter connects, created its six tables in the configured database, and round-trips a value. It restoresCONNECTand drops the fixture afterwards, so it is self-contained and CI-portable.master+ this testpermission denied for database "postgres" (code: 42501), "adapter never reported a connection"["datapoints","sources","ts_bool","ts_counter","ts_number","ts_string"],getHistoryreturns the written4711Worth noting: a test that enables this option as the superuser passes either way, because the superuser can open
postgres— which is why the test uses a restricted role.npm run check:tsandnpx prettier --checkclean (including the new test file —npm run lintdoes not covertest/**/*.js). (npm run lintreports 3 errors insrc-admin/, all pre-existing on unmodifiedmasterin files this PR does not touch.)Relation to #561
This is the other half of the same original change; #561 covers the
info.connectionreporting (#374). They are independent and can land in either order.🤖 Generated with Claude Code