0
engine = create_engine("postgresql+psycopg2://(username):(password)@(hostname):(port)/(dbname)",connect_args={'sslmode':'true'})

I tried above code while using cdk deploy got error from aws which is :

[ERROR] OperationalError: (psycopg2.OperationalError) invalid sslmode value: "true"

Also, connect_args={'sslmode':'require/prefer'} is not working as :

FATAL:  SSL connection is required. Please specify SSL options and retry

2 Answers 2

4

This is the right syntax:

engine = create_engine(SQLALCHEMY_DATABASE_URI, connect_args={'sslmode': "allow"})

You can also change the kind of sslmode. For more info pls check here.

Please be aware that your Postgresql server needs to support ssl connections if you want to be able to connect through it. For this reason you have to run it with ssl=on option and also specify certificates to use.

0

Sorry for being late to the party. For postgresql you use use the same parameters (mostly) you'd use on the command line, through a configuration variable:

    SQLALCHEMY_ENGINE_OPTIONS = {
        "connect_args": dict(host=os.environ['POSTGRES_HOST'], user=os.environ['POSTGRES_USER'],
                             password=os.environ['POSTGRES_PASSWORD'], dbname=os.environ['POSTGRES_DB'],
                             sslcert="client.crt", sslkey="client.key",
                             sslrootcert="ca.crt", sslmode="verify-full", ssl_min_protocol_version="TLSv1.3")
    }

Whenever you create the connection, these arguments will be picked up. You might have problems generating the self-signed ssl certificate for local testing: using names on the CN will prevent this connection line to succeed with 'verify-full'. In that case, the options are to revert to just 'require', to upgrade to 'verify-ca' (ca is verified but not hostname), or to generate the certificates with 127.0.0.1 (or the corresponding ip) everywhere... and then it will succeed with verify-full.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.