Connecting to CloudSql via psycopg2 - psycopg2

I am trying to connect to CloudSQL on GCP using pycopg2
In order to do this I use as host:
'my-project-dev-a24525:europe-west1:sql-dev-432521ec-master'
as given in "Instance connection name"
conn = psycopg2.connect(host='my-project-dev-a24525:europe-west1:sql-dev-432521ec-master',
database="postgres", user="admin", password="password")
However this results in:
could not translate host name "my-project-dev-a24525:europe-west1:sql-dev-432521ec-master" to address: Temporary failure in name resolution
If I connect via the public ip address as in
conn = psycopg2.connect(host='xx.xxx.xx.xxx', database="postgres", user="admin", password="password")
the connection is fine.
Where can I find the proper hostname in GCP?

Your Cloud SQL instance doesn't have a DNS name associated with it (unless you give it one).
The "instance connection name" which you have listed can be used to connect with the Cloud SQL Proxy or when interacting with the Cloud SQL Admin API, but doesn't have any DNS significance.

Related

Azure data factory problem to write data from a Postgresql in private Vnet to Azure SQL Database

I am trying to retrieve data from a table located in a postgresql database hosted on azure VM. I would like to inject this data into an Azure SQL Database. I set up the self hosted integration runtime. Since the Vm hosting my DB is a linux VM, i have setup self-hosted IR on windows VM and using that I am able to connect to DB and copy the data to ABS location without any problem but when I try to copy the same data to SQL database, I am getting following error:
ErrorCode=SqlFailedToConnect,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=Cannot connect to SQL Database: 'XXX', Database: 'XXX', User: 'XXX'. Check the linked service configuration is correct, and make sure the SQL Database firewall allows the integration runtime to access.,Source=Microsoft.DataTransfer.ClientLibrary,''Type=System.Data.SqlClient.SqlException,Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.),
Source=.Net SqlClient Data Provider,SqlErrorNumber=10060,Class=20,ErrorCode=-2146232060,State=0,Errors=[{Class=20,Number=10060,State=0,Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. ...
After searching about I opened the inbound and outbound traffic for port 1433 on machine which is hosting IR and and outbound traffic for port 1433 on machine which is hosting the database but I am still getting the same error.
Any idea what am I missing here?
I managed to solve the problem. As per azure documentation here, if we are trying to connect to SQL database from within Azure cloud then we need to open communication to port range 11000-11999. We need to put this port range in network security list for outbound connections.

Connection refused when attempting to connect to cluster in private VPC from local PC

Redshift noob here coming from BigQuery. I have no experience with networking/CIDR/Firewalls/Routes/VPC.
I am trying to connect to my redshift cluster (single node) from my local computer using the python package psycopg2.
When I try to connect, I get the error:
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "my-cluster.some-random-stuff.us-east-2.redshift.amazonaws.com" (172.31.xx.xxx) and accepting
TCP/IP connections on port 5439?
From this post, I added the above private IP address (172.31.xx.xxx) as well as my local IP address to the security group inbound Rules (VPC > Security > Security Groups), but that didn't work.
I also followed this documentation, and added the routes 0.0.0.0/0 and ::/0 in my routes table. This didn't help either.
What am I missing?
Here is my code:
def connect_redshift():
client = boto3.client('redshift', region_name=REGION_NAME)
credentials = client.get_cluster_credentials(DbUser=USER,
DbName=DATABASE_NAME,
ClusterIdentifier=CLUSTER_ID,
AutoCreate=False)
try:
conn = psycopg2.connect(
host=HOST,
port=PORT,
user=credentials['DbUser'],
password=credentials['DbPassword'],
database=DATABASE_NAME
)
return conn
except psycopg2.Error:
logger.exception('Failed to open database connection.')
print("Failed")
Using the aws configure, I added my access key ID, and my secret key. I have AdministratorAccess permission.

Azure Runbook to connect to SQL server running on Azure VM

I am trying to connect to a SQL server running on an Azure VM using the below sample code (having defined the SQL user name and password):
$SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialAsset
# Get the username and password from the SQL Credential
$SqlUsername = $SqlCredential.UserName
$SqlPass = $SqlCredential.GetNetworkCredential().Password
# Define the connection to the SQL Database
$Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer,$SqlServerPort;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;")
this code works when I am connecting to Azure SQL Database , but not when connecting to SQL running off Azure VM.
the error thrown is:
Error occured: Exception calling "Open" with "0" argument(s):
"A network-related or instance-specific error occurred while establishing a
connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider, error: 0 - No such host is known.)"
I have verified on the VM SQL remote connections are enabled and including TCP port 1433 defined.
Probably it's a network related issue. If you have verified on the VM SQL remote connections are enabled and including TCP port 1433 defined. Also, the port 1433 was opened in the inbound port rule of NSG attached to the VM and windows firewall inside the VM. You could use telnet verify this port locally.
Moreover, you could specific $SqlServer= "sqlvmlabel.eastus.cloudapp.azure.com"which is a public DNS name of Azure VM. Here is an example of a connection string for using SQL Authentication.
Server=sqlvmlabel.eastus.cloudapp.azure.com;Integrated Security=false;User ID=<login_name>;Password=<your_password>
Read more information: connect to a SQL Server Virtual Machine on Azure

zabbix with postgresql on AWS RDS

I have one EC2 instance and when I check connection with psql tool by it is OK.
psql --host= etc...
Basically, AWS RDS does not provide internal IP for connection. I have to use long URL Endpoint instead.
How to provide this endpoint address to zabbix web interface installation tool?
When I use endpoint in "Database host" it fails :(

Can't connect to azure sql via ssms

I'm trying to connect Azure's SQL database via SQL Server 2008 R2 SP2. I'm using Azure with free trial subscription. I already search everywhere and tried these things:
Allow inbounds and outbounds rule in windows firewall for port 1433
Check that TCP connections in SQL Server Configuration Manager is available and running on port 1433
Create server and database on azure portal and configure its ip allowance
I connect with these settings
Server Type: Database Engine
Server Name: tcp:aaa.database.windows.net,1433 / aaa.database.windows.net
Login: bbb#aaa
Password: xxx
And the error is
"A network-related or instance-specific error occurred while
establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the
instance name is correct and that SQL Server is configured to allow
remote connections.
(provider: TCP Provider, error: 0 - A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond) (Microsoft SQL Server, Error: 10060)"
P.S. I also can't ping xxx.database.windows.net and can't telnet xxx.database.windows.net 1433
Your question is confusing, as you're talking about both SQL Server and SQL Database. But, given the connection strings you mentioned (xxx.database.windows.net) you are definitely talking about SQL Database service, not SQL Server in a VM.
That said: You must allow certain IP addresses to connect to it (this is a built-in part of the service itself). Look at the SQL Database server's Firewall setting:
You'll need to add the IP address of your computer (or IP range), to access SQL Database through your local tools.

Resources