-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Django 'Invalid Argument (0x00002726/10022)' Error When Connecting to Google Cloud SQL via Unix Socket with IAM Authentication #1153
Comments
Hi @RaulMaya thanks for raising an issue on the Cloud SQL Python Connector! 😄 There are a couple quick questions I need to understand your use-case before I can assist properly.
Answers to these two questions will help me tailor the guidance on your best path forward. However, I can give a bit of clarity/help as is now.
Setting your If your Cloud SQL instance is configured with a Private IP (recommended) and your application is deployed to Google Cloud then the recommended path forward would be to just connect directly by setting your DATABASES = {
'default': {
'ENGINE': 'django_gcp_iam_auth.postgresql',
'NAME': 'my_db_name',
'USER': 'user@email',
'PASSWORD': '',
'HOST': 'X.X.X.X', # your Cloud SQL instance's private IP address
'PORT': '5432', # Postgres default port
'OPTIONS': {
'gcp_iam_auth': True,
'sslmode': 'require', # should always be set to 'require' for IAM AuthN
},
}
} Hope this helps a bit, once I know more about your deployment/configuration I can help more 😄 Related to #437 |
Hello @jackwotherspoon thanks for addressing this issue, regarding the questions:
DATABASES = {
'default': {
'ENGINE': 'django_gcp_iam_auth.postgresql', # Use the custom engine
'NAME': 'pareto_db_1',
'USER': '[email protected]',
'PASSWORD': '', # Password is managed via IAM, so leave it empty
'HOST': '10.XX.XXX.141', # Use the correct Cloud SQL connection name
'PORT': '5432', # Typically leave as empty string for Unix socket connection
'OPTIONS': {
'gcp_iam_auth': True, # Enable IAM authentication
'sslmode': 'require', # Set SSL mode as needed (e.g., disable if using Unix sockets)
},
}
} Additional Data:
I use the suggested snippet, but I'm encountering with an error: |
@RaulMaya This is a promising error, it means your connection is actually reaching the Cloud SQL server.
Is this missing an "8" in the username? I am going to move forward assuming this is just a typo and was the proper username in your code.
This is most likely the cause of your error. Both of these steps do the same thing, set your Application Default Credentials (ADC), if you do them both then the second one will override the first. If you are setting Let me know if the above paragraph makes sense, I can try to clarify if need be. |
Hello @jackwotherspoon Thanks for the huge help, it finally worked, and it was exactly what you wrote, the I will close this issue, with this comment. Thank you :) |
Question
How Do I Correctly Connect Django to Google Cloud SQL via Unix Socket with IAM Authentication?
Code
Additional Details
I'm trying to connect my Django application to a PostgreSQL instance hosted on Google Cloud SQL using a Unix socket and IAM authentication. Here’s my configuration in settings.py.
Actually I'm using the suggested repo for this authentication: https://github.com/rcleveng/django_gcp_iam_auth, that @rcleveng post
The error I'm getting is:
django.db.utils.OperationalError: connection to server on socket "/cloudsql/company-XXXXXXXXXXXXXXXX:us-central1:pareto-instance-prod-XXXXXX/.s.PGSQL.5432" failed: Invalid argument (0x00002726/10022)
Is the server running locally and accepting connections on that socket?
The text was updated successfully, but these errors were encountered: