PostgreSQL version in connection string - node.js

I have two versions of postgresql installed, 9.4 and 9.5. I am using npm's pg to connect to the server using a postgres connection string. From CLI, I can connect to 9.5 by using the --cluter option like so: psql --cluster 9.5/main. In postgres' documentation for the connection string, they show one of the querystring parameters you can use is options:
options - Adds command-line options to send to the server at run-time
I realize this means configuration parameters for the server, not the client. Nonetheless, I tried constructing the connection string and including the cluster option to connect to 9.5:
postgres://user#localhost/db_name?options=--cluster%3D9.5%2Fmain
And sure enough I get: psql: FATAL: unrecognized configuration parameter "cluster"
Okay, no surprise there. However, I can't seem to connect to 9.5 no matter what I do. I even have the environment variable PGCLUSTER set to the appropriate version string, however I think this is only read from CLI (ie: not when the server is connected to via unix socket or host).
What is the easiest way to connect to the 9.5 server instead of 9.4 from a script connecting to the server via localhost?

The best approach is to find the port that the 9.5 server is running on. In my case, 9.4 was running on port 5432 whereas 9.5 was running on 5433. This is a simple change to the connection string:
postgres://user:pass#localhost:5433/db_name

Related

connecting to mongo srv string using mongoose nestjs with ssh tunneling

I am trying to do ssh tunneling and connect to my localhost which would forward my request to atlas cluster.
Note: My mongo connection string is srv string
Steps I followed.
1. ssh -N -L 27017:cluster0.acsacasc.mongodb.net:27017 -i /Users/cvrg/.ssh/id_rsa ubuntu#2.1.5.2
2 a. connect to string mongodb+srv://cd-cdd:cadcdacaca#localhost/dev?retryWrites=true&w=majority using mongoose/nestjs
2 b. connect to string mongodb://cd-cdd:cadcdacaca#localhost:27017/dev?retryWrites=true&w=majority using mongoose/nestjs
2 c. connect to string mongodb+srv://cd-cdd:cadcdacaca#localhost:27017/dev?retryWrites=true&w=majority using mongoose/nestjs
Nothing out of the above seems working and i didn't found anything which shows how to do tunneling for srv cluster string.
Please help
Note: I tried same with compass, and successfully able to connect but getting issues with terminal and nodejs
Anyone else having hard time doing this, i end up using https://github.com/sshuttle/sshuttle for tunneling/vpn.
Steps to use this:
1. connect to your jumper/bastion server using ssh.
2. sudo sshuttle -r user#jumper_ip 0/0 -vv
Voilla now you can use the same connection string provided by atlas cluster and would be able to connect with it.

SSH tunnle to mongodb using mongodb connection string

Thought it should be straight forward but I have a very hard time figuring out the following:
I got mongodb connection string: mongodb://user:password#123.123.123.111:27017/?authSource=admin
I want to be able to connect to mongo from localhost at port 1234 by doing: mongo localhost:1234
The solution is to create a tunnel, but nothing I do works for me.
I tried the following command:
ssh -L 1234:localhost:27017 user:password#123.123.123.111 -p 27017
Please help me understand what I am doing wrong.
You need to have a unix user on 123.123.123.111
ssh -L 1234:localhost:27017 UNIXuser#123.123.123.111
Then your local mongodb connection string is : mongodb://user:password#localhost:1234/?authSource=admin
MongoDB and ssh use different protocols, so you can't use ssh to connect directly to a mongod process.
If you want to use an ssh tunnel you will first need to have an account on the destination machine, and use that account's credentials with ssh to connect to port 22 (assuming default port). The mongod username/password will probably not be valid for ssh.
Once the tunnel is established, you would connect to the local port using a driver or mongo shell using the connection string:
mongodb://user:password#127.0.0.1:1234/?authSource=admin

pg 7.18.0 in node js not working, throwing: Implicit disabling of certificate verification is deprecated and will be removed in pg 8 [duplicate]

I'm using Macbook
Psycopg2 works well when connecting the localhost db (PostgreSQL on Mac). The error was raised when I tried to connect PostgreSQL db on a Windows10.
the following code is what I have for connection, the host is just the IP of the windows10
db= psycopg2.connect(database='dbname',user='username',password="secret",host="192.168.3.9",port="5432")
Errors:
File "path/to/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: unsupported frontend protocol 1234.5679: server supports 2.0 to 3.0
Is this because of system compatibility or something else? I've tried other Windows machine and I got no luck to make it work.
However, I was able to connect PostgreSQL on windows while I using Node.JS module pg
1234.5679 is the special code sent by the client to request SSL-encrypted database connections, and support for that has been in PostgreSQL since commit e0e7daef6da in 1999. But your PostgreSQL cannot be that old, because support for protocol version 3.0 was not added before 2003.
Actually, from studying src/backend/postmaster/postmaster.c and reading the mailing list, this is a bug on the PostgreSQL server:
The client must be configured to try GSS authentication, and when the server rejects, it wants to negotiate an SSL connections, but the server doesn't expect that at this point; hence the error.
See the discussion here. The bug has been fixed with release 12.3.
As a workaround, disable either GSS authentication or SSL negotiation on the client.
In psycopg2, disabling SSL is done by using sslmode="disable" in the connection string, and disabling GSS is done with gssencmode="disable". See the documentation for details.
Adding ?gssencmode=disable to the connection string worked for me:
import pyodbc
from sqlalchemy import create_engine
engine = create_engine(f'postgresql://{user}:{password}#localhost:5432/database_name?gssencmode=disable')
Getting a similar error working with Laravel and Postgres. Solved it by putting this in my .env file: PGGSSENCMODE=disable
If you installed this Psycopg2 module through conda command, then uninstall that module and install using pip command.
Command:
pip install Psycopg2
The above command may resolve your issue. I resolved through this step
In order to have a SSL connection working and using certificates, you can disable GSS connection mode to avoid the client to attempt to connect with GSS and directly try a SSL connection (Postgres version 12 < 12.3, I had no such issues with a test on version 11).
Below an example with the verify-ca option, providing filenames for certificates and key (example names from GCP Cloud SQL, running Postgres 12.1 when posted).
from sqlalchemy import create_engine
db_user = 'user'
db_pwd = 'secret'
db_host = 'hostname'
db_port = '5432'
db_name = 'test'
cnn = f'postgresql://{db_user}:{db_pwd}#{db_host}:{db_port}/{db_name}'
ssl_args = {
'gssencmode': 'disable',
'sslmode': 'verify-ca',
'sslrootcert': 'server-ca.pem',
'sslcert': 'client-cert.pem',
'sslkey': 'client-key.pem',
}
engine = create_engine(cnn, connect_args=ssl_args)
This engine can then be used with pandas for example:
df.to_sql('my_table', con=engine)
Using PostgresSQL 13.0 I had the same problem, displaying the error messages:
unsupported frontend protocol 255.255: server supports 2.0
unsupported frontend protocol 0.0: server supports 2.0 to
according to this site it postgresql.org deals with the SSL / GSS Protocol Negotiation Problem. Which should already be resolved in Postgres version 12.0
I was able to solve it following the guidelines contained in these sites:
highgo.ca
In the postgres terminal, I executed the command below setting the environment variable gssencmode = disable and the problem was solved:
psql -h localhost -U postgres -d "dbname=belez gssencmode=disable";

MongoDB: No unix socket support on windows

I use Robo 3T as a UI tool for MongoDB on a Windows 8 machine. Now I've deployed the DB to production on a Ubuntu 14 server in Amazon EC2 cloud. When I try to connect from the Windows machine to the Ubuntu one via Robo 3T, I receive the following error:
Cannot connect to the MongoDB at [http://12.345.678.90]:27017.
Error: No unix socket support on windows
Is that a problem I need to fix on my computer by installing something? or it's an issue of Mongo 3T?
If it's an issue on my Windows, what do I have to install in order to make it work?
If it's a Mongo 3T issue, do you know another UI that does support connecting from Windows to Ubuntu?
Perhaps an EC2 solution?
Or maybe some settings I need to change in Robo 3T?
I've tried changing "http" to "mongodb" to no avail. I've also tried removing the protocol prefix as suggested here but I ended up with the error:
Cannot connect to the MongoDB at 12.345.678.90:27017.
Error: Network is unreachable.
Do you have a bindIP setting in your mongo configuration file? Probably that can cause the error.
Either remove the bindIp configuration or allow your IP to access mongo server.
(restart your service after changes the configuration).

Accessing MongoDB from Windows & Mac Client Machines

I have MongoDB 3.2 installed on my Linux Red Hat server.
I am starting to access it and looking at the mongo Shell instructions.
For a Windows machine, the instructions want me to get to the command prompt and change dirs to the installation directory. The problem is, MongoDB is installed on my web server and not my local windows machine.
Question: does Mongo Shell apply to me then? How do I start using, connecting and accessing Mongo from my Windows and Mac machines?
[Note: I am a traditional MySQL / phpMyAdmin developer looking to advance to MongoDB]
Amendments:
(1) With the help of #AlexBlex I am progressing to trying to connect to my MongoDB on my server from Robomongo on my windows client. I get the following error when trying to setup my connection. I tried the address with just my server ip and with http://{my server ip}. Neither worked. See screen shot of error
(2) This is what I have in my current mongod.conf file:
#port=27017
bind_ip=127.0.0.1
(3) here is what my connection settings look like. Oddly, #AlexBlex's solution below shows an SSH tab on his Mac version. The Windows and Mac versions I just installed lacks that tab.
If you install MongoDB on your local machine, you can use the Mongo shell like below to connect to your remote server
mongo yourserver:27017/database
You will have to configure your Mongo server to allow remote connections. In order to achieve this you need to have the following line in your /etc/mongodb.conf file. You need to replace 10.0.0.2 with the ip address of your client machine.
bind_ip = 127.0.0.1,10.0.0.2
You need either ssh to the server where mongodb is installed, or install mongodb on local machine.
For robomongo to connect to remote host you need to ssh to the server, and check it listens on external interface:
lsof -i | grep 27017
In case it is bound to localhost only, you need to edit a line with bind_ip in /etc/mongodb.conf and restart the service.
I would recommend to keep it listening on the local interface only for security reasons, and use ssh tunnelling to connect:
I found the answer. #ShahNewasKhan is brilliant. See How to connect Robomongo to MongoDB
All you need to do is SSH to server and edit mongod.conf file:
uncomment #port=27017 to port=27017
comment bind_ip=127.0.0.1 to #bind_ip=127.0.0.1
restart mongodb via service mongod restart
Then create a mongo connection via your server ip in the address field and 27017 in the port field
Hope this helps mongo newbies and start-ups like me :) Good luck.
Now I just need to figure out how to make this secure. My concern is that anyone who knows my server ip can hack into my MongoDB

Resources