Where’s this 2nd DB located? Is it inside or outside Docker?
Each Docker container only runs one service. So, the NodeJS container only runs NodeJS, there’s nothing else running at localhost, so you cannot connect to a database running on localhost.
When Wappler creates a NodeJS and Database inside Docker, those are two containers. The NodeJS doesn’t talk to “localhost”, it talks to “db” (which is DNS resolved to the container’s IP address that runs the database)
Are you running this database inside Docker? If yes, they you most likely modified the docker-compose file (e.g.: added a service called “db2”), so you use “db2” instead of localhost.
Are you running this database outside Docker? If yes, this is a more difficult scenario as it depends if you’re running Windows/MacOS/Linux, there are different methods to connect from a container to the host system. If possible, I suggest running everything inside Docker for simplification purposes
You can edit the docker-compose.yml file in your project folder, e.g.:
Yes, just for testing purposes, the 2nd DB is located inside the same docker container. It is the the same database connection, i.e. its the same database instance in Docker, but within that instance you can clearly create multiple databases.
And here is that same database instance with multiple DBs created inside it when i connect through Navicat (or indeed Wappler DB manager:
So the 2nd database has indeed been created in the same docker container and does work, just not with any server connect action.
I’ll look into what else you’ve said as i’m sure it’s an error on my end and perhaps not how the docker and Wappler are supposed to work in regards to multiple DBs
Ah, I understand it: you have two databases inside the same one database server
I was thinking you had two PostgreSQL containers, but it’s just one.
So, the issue is, you cannot use “localhost”, because on localhost it’s just the NodeJS server running, not the PostgreSQL. The PostgreSQL is in another container, whose name is “db” (or “db_1”)
So you need to use “db” instead of “localhost” (Docker will take care of resolving “db” to the container’s IP address running Postgres)
This may sound a bit confusing:
You can use “localhost” on the Wappler UI, for the database manager, because the Postgres container has a published port 9906 (so you can connect using Navicat to localhost:9906)
You cannot use “localhost” inside the NodeJS app, because in the NodeJS Docker container it’s just NodeJS and nothing else running - NodeJS has to talk to another machine (container): db
You need to think like having one computer running NodeJS, and another computer running PostgreSQL. On NodeJS computer you cannot use localhost for accessing PostgreSQL - it’s not running there
You are best setting a second DB up remotely if you can. As @Apple suggests trying to connect to 2 localhost DBs can cause problems.
The solution I came up with on the attached post works fine and I continue to use it. There is no dynamic picking in the Wappler UI so you do have to edit the Global DB connection file in code view to add the {{hostname}}, {{dbpassword}} etc. values.
One thing that I have done, is set the static ‘Direct’ DB connection for DB Manager to a fixed address of one of the DBs. This allows creating queries and working with Wappler without having to have separate Test and Dynamic connections under Globals.
Just to clarify, there's no problem using 2 localhost DBs It's just an issue of understanding how Docker container works and their network specificities (why using "localhost" doesn't work, but "db" works)
On Wappler database manager it's not possible to use "db" - because that only exists within a Docker container, and Wappler database manager is not running inside a Docker container. In that case, "localhost" needs to be used instead
Great, thanks for this…I had started to think this was the case but I think you’ve clarified it for me and helped make a bit of sense out of it.
One more thing though, because your understanding is correct in how I’ve got it set up, where in Wappler should I be editing the config details to connect to host db?
Presumably not in the db manager ui?
There are a number of other files that have the details, sucha as the db connection files or in server connect db connection settings etc?
Yes, Wappler organizes files a bit weirdly. When you change the DB details through “Database Direct Connect” in the Wappler database manager, it’s not changing for the NodeJS app
There’s a .json file for the Wappler database manager, and there’s another .json file for the NodeJS app. These are the ones for NodeJS:
app/modules/connections/
I don’t remember if it’s possible to change through the UI - I think so, but there you have the JSON you have to edit
Ok… If anyone comes back to read this, this is how i was able to get it work easily using just the Wappler UI.
Step 1.
Using the Database Manager, create new connection.
Enter the relevant database name and user credentials etc
KEY = Make sure that the host is set to db and the port is set to 5432
DB and port 5432 may change based on the docker settings but this is what Wappler would create by default.
This will create a new DB connection but it WONT actually be able to connect to the DB, it will however create the two JSON files with relevant DB connection info.
Step 2
Once the above has saved, open the connection details and change the host to localhost and the port to 9906 (again, these are just the default details, yours may vary)
Hit save and this will then only update one of the JSON files, the one with the DB connection details for DB manager.
The JSON file used for server connection actions will still have the connection details with host db and port 5432