Search code examples
node.jspostgresqlsequelize.jsgoogle-cloud-sqlcloud-sql-proxy

Sequelize-cli migration with non-standard unix sockets


I'm trying to get sequelize going on GAE. But am struggling with the cloud SQL proxy that uses sockets on non-standard paths.

My understanding is that a url would get passed direct to the pg library, and based on this post, it would suggest the below string is what I need

I've setup the db configuration to be

{
  url: 'postgresql:///db_user:db_password@/db_name?host=./cloudsql/my-project-1234:us-central1:my-cloud-sql-instance', 
  dialect: 'postgres'
}

However, if I run ./node_modules/.bin/sequelize db:migrate I end up with the error:

ERROR: database "db_user:db_password@/db_name" does not exist

Solution

  • After a lot of trial and error, it is possible to pass the socket path using the host variable, but there are a few gotcha's:

    1. The path must be absolute, that is, it must start with a / (otherwise it assumes it is a hostname)
    2. The socket path must be 108 characters or less on a Linux-based operating system. If I placed the socket in a subfolder with my project (which was in my home directory), I got ENOENT (file not found), but moving the sockets to /cloudsql in the root path fixed it.
    3. It will assume that path is the directory and append /.s.PGSQL.5432 to that (which is fine because Google's SQL proxy socket is in that file)

    So with the following config, I was able to run a database migration through google's cloud SQL proxy:

    {
        username: 'my_user',
        password: 'my_password',
        database: 'my_database',
        host: `/cloudsql/my-app-12345:us-central1:my_database`,
        logging: false,
        dialect: 'postgres',
        // Google cloud_sql_proxy is encrypted, so no need to encrypt
        ssl: false,
        dialectOptions: {
            ssl: false,
        },
        operatorsAliases: false,
    }