Specifying the DB to connect with gcloud sql - google-cloud-sql

When connecting to a Google Cloud SQL instance, is there a way to specify the database I want to connect to?
This is the command I execute and the error I get:
$ gcloud beta sql connect MY_INSTANCE --user=MY_USER
Whitelisting your IP for incoming connection for 5 minutes...done.
Connecting to database with SQL user [MY_USER].Password for user MY_USER:
psql: FATAL: database "MY_USER" does not exist
It seems to try to connect to a database that has the same name than the user, but what when that's not the case? (I don't want to create a dummy DB called MY_USER just to be able to jump to the DB I want).
Here are the docs for the command gcloud beta sql, but I don't see anything useful for this there: https://cloud.google.com/sdk/gcloud/reference/beta/sql/connect

gcloud sql connect does not currently allow you to specify the database name. There is a open issue to add it: https://issuetracker.google.com/issues/69314375
In the meantime, you can work around this by utilizing the PGDATABASE environment variable:
e.g.
PGDATABASE=mydb gcloud sql connect myinstance
You could create a shell alias to do this for you.

In the meanwhile the beta version of the gcloud sql connect command has added support for a --database=<dbname> flag
Entire command:
gcloud beta sql connect INSTANCE [--database=DATABASE, -d DATABASE] [--user=USER, -u USER] [GCLOUD_WIDE_FLAG …]
https://cloud.google.com/sdk/gcloud/reference/beta/sql/connect

Related

How to create local clone of a GCP Cloud SQL export for PostgresQL

I have exported a database as a SQL file in Google Cloud Platform Cloud SQL Postgres Database using their GUI wizard. I want to import the data from the dump into my local PostgresQL installation. I tried using PgAdmin4 with various settings but kept failing. I then tried the following command which retrieved the data but most of the relationships are gone.
psql -f "SQLFile.sql" --host "localhost" --port "5432" --username "dbusername" "myDBName"
How can I create a local clone of my Cloud SQL db?
Posting an answer for anyone else who needs this. The following was performed on a Windows 10 machine.
Export from Cloud SQL using the GCP console GUI. This will save it to a cloud storage bucket and you can download it from GCS bucket.
On local machine - remove any database with the same name as the one you want to import . Then create a fresh database with the same name as the one you want to import. (in my case this was the issue)
In terminal go to your PostgreSQL bin folder. Example:
cd “C:\Program Files\PostgreSQL\13\bin
Then run the following command - replacing values as per your needs.
psql -f "<full-path-to-downloaded-sql-file>" --host "localhost" --port "5432" --username "<local-postgres-username>" "<db-name-to-import>”
You may need to grant privileges to users on tables. This can be done using the grant wizard in pgAdmin 4 or as described in this answer.

UnhandledPromiseRejectionWarning: SequelizeConnectionError: database "dbname" does not exist

While trying to connect to a DB instance running the PostgreSQL database engine on Amazon RDS.
I have checked the environment variables and the values are set correctly.
However, when I run npm run dev I encounter the error below
(node:32031) UnhandledPromiseRejectionWarning: SequelizeConnectionError: database "database-1" does not exist
My DB is publicly accessible and security groups have been checked to be correct(allows inbound and outbound connection).
Was able to solve this by using the default database name postgres for the env --dbname option.

Fusionauth-app docker without mysql superuser credentials

I would like to connect to a hosted remote mySQL DB (mariadb 10.1.39). I use the available fusionauth docker images (app and search) from docker hub and the published docker compose file. The db hosting provider does not grant superuser credentials. The assigned user rights should be sufficient to maintain the tables of the schema. Unfortunately, using the docker container mysql superuser credentials seems to be mandatory.
I imported the DB dump of a local (dockerized) mariadb (10.1.40) to the remote db. Username and schema name are the same local and remote. I tried not to provide DATABASE_ROOT_USER with the docker-compose yaml, but this approach ends in maintenance mode.
Is there an approach to connect to a remote mysql db without superuser credentials?
We will be enhancing our automated setup to better support external db service providers. See https://github.com/FusionAuth/fusionauth-issues/issues/95
Your current option is to create the schema manually. https://fusionauth.io/docs/v1/tech/installation-guide/fusionauth-app#advanced-installation
You may also try to use your user credentials in the superuser fields, it may work.

.pgpass with AWS RDS

I need to run multiple commands on an AWS Postgres RDS instance I have. I don't want to enter the password each time. I'm trying to use the .pgpass file but I'm running into errors. The first time I ran into an error which said 'role "ubuntu username" does not exist'. I logged in as the postgres user and created that username. After this, the error I get said database does not exist. I have a feeling these errors have nothing to do with trying to connect to the AWS RDS instance.
psql --host=<awshost> --port=5432 --username=<awsrdsusername> --password --dbname=<dbname asks for a password and then logs me in after I enter it. Now I put a file in /home/<ubuntuusername> called .pgpass which has <awshost>:5432:<dbname>:<awsrdsusername>:<password>. Permissions for this file are set to 0600. Now when I run psql from the terminal and that produces the error - psql: FATAL: role "<ubuntuusername>" does not exist. These steps are as outlined on this page.
Can someone help me with the steps to get a pgpass file to connect to an AWS RDS instance?
.pgpass doesn't provide connection information. You seem to expect that after you create a .pgpass file, you can run psql without arguments and it'll know where to connect. That is not the case.
The hostname, port, etc you put in .pgpass are there so that PostgreSQL knows which line to look at when matching the connection info to find its password.
If you had to run:
psql -h something
to connect without a .pgpass file, you still have to run the same thing to connect with a .pgpass file.
If you run psql without arguments it'll connect to the local PostgreSQL (if any), using the current unix username as the postgresql username and the database to connect to. That's why you get the error you do.
If you want to change the default connection, you can use environment variables like PGHOST, PGPORT, etc, and/or a .pgservice.conf file.
See the manual to learn more.

Postgresql creating database

Well I installed the latest postgreql database on my Windows 7.
Now I'm trying to create a database via the psql.exe command line
When I open it, it says
psql: FATAL: database "Jansu" does not exist
So I read somewhere, that when no database is specified, it tried to find database with my username or something.
Anyways..how do i create a new database, when I can't access the commandline.
Read psql syntax. You can specify database, user and other parameters. If it's a new installation, there should be a default database 'postgres', you can connect to that one.
psql -U postgres postgres
(In Unix environments you might need to add -h localhost in order to force a TCP connection, otherwise it'd try to use Unix-domain sockets, which might not work for other than the postgres user. )
You can create databases from there, or from the command line with createdb