The dreaded AWS error : Cannot find version xx.x for postgres
Tags: javascript, aws, serverless, postgres
Published: 2022-03-17 02:52:10

When creating an AWS stack containing a postgres database table, you might received this error:

#Error

`Cannot find version 12.3 for postgres (Service: AmazonRDS; Status Code: 400; Error Code: InvalidParameterCombination`

---
`Cannot find upgrade path from 14.2 to 12.3. (Service: AmazonRDS; Status Code: 400; Error Code: InvalidParameterCombination`

###Erroneous code 
```javascript
const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_12_3 });
```

Now this error code : **InvalidParameterCombination**, and the the  message: `Cannot find version 12.3 for postgres` is not very explicit and doesn't explain what the real issue is. 

What the error really means is that version **12.3** for postgres is depreciated and any reference to that version, or any version you are having an issue with, should be removed from your code and replaced with a supported version.

#The real issue
AWS requires that you specify the version number for the database when it is being created. However, AWS doesn't keep all versions of that database around indefinitely. 

AWS depreciates certain versions, so at some point in the future you will have to **change the code to use a supported version** of that database.

---

 
#How to fix the error?

**Specify a supported version of the database**


###Fixed code: version 1

| <b>Simple code</b>|
```javascript
const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_14_2 });
```


###Fixed code: version 2
Use version 2 of the fix, if you don't like to hardcore values in your code that are expected to change.

| <b>Superior code fix</b>|

```javascript

/**
* Store values in environment variables
* @see https://www.npmjs.com/package/dotenv
*/
const postgresFullVersion = process.env.POSTGRESFULLVERSION ?? "14.2";
const postgresMajorVersion = process.env.POSTGRESMAJORVERSION ?? "14";

/**
* Generate the postgres engine version from the values in the environment variable
*/

const engine = DatabaseInstanceEngine.postgres({
    version: PostgresEngineVersion.of(
      postgresFullVersion,
      postgresMajorVersion,
    ),
  });
```

---


#How to know which versions of the database is Depreciated and supported


- Visit the AWS website 

[https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html)


| ![Depreciated versions of postgres database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vr32qz2nfemoiljow45r.png) |
|:--:|
| <b>Image: Depreciated 12.x versions of postgres</b>|

- or Run the [AWS CLI](https://aws.amazon.com/cli/) code below 

After running the command, scroll all the way to the end of the list to see the most recent version of the database supported by AWS

```bash
aws rds describe-db-engine-versions --engine postgres --output table 
```

|![AWS CLI view of current version of postgres](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/awaq8k3ikz51hemmxcv8.png)|
|:--:|
| <b>Image: AWS CLI view of current version of postgres</b>|

 

---

### AWS stack creation code (contains error)

```javascript
import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Vpc, InstanceType, InstanceSize, InstanceClass } from 'aws-cdk-lib/aws-ec2';
import { DatabaseInstance, DatabaseInstanceEngine, PostgresEngineVersion } from 'aws-cdk-lib/aws-rds';


class CdkRdsStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const vpc = new Vpc(this, 'TestVpc', {
      cidr: '10.192.0.0/16',
      maxAzs: 2,
      natGateways: 1,
      enableDnsHostnames: true,
      enableDnsSupport: true
    });

    //Erroneous Line 
    //--------------------------
    const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_12_3 });
    //--------------------------
    
    const dbInstance = new DatabaseInstance(this, 'Test_Postgres_CDK', {
      engine,
      vpc,
      iamAuthentication: true,
      instanceType: InstanceType.of(
        InstanceClass.BURSTABLE3,
        InstanceSize.MICRO
      ),
      databaseName: 'test_postgres'
    });

    new CfnOutput(this, 'dbEndpoint', {
      value: dbInstance.instanceEndpoint.hostname,
    });
  }
}
```

---


### AWS stack creation code (fixed code)

```javascript
import { Stack, StackProps, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { Vpc, InstanceType, InstanceSize, InstanceClass } from 'aws-cdk-lib/aws-ec2';
import { DatabaseInstance, DatabaseInstanceEngine, PostgresEngineVersion } from 'aws-cdk-lib/aws-rds';


class CdkRdsStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const vpc = new Vpc(this, 'TestVpc', {
      cidr: '10.192.0.0/16',
      maxAzs: 2,
      natGateways: 1,
      enableDnsHostnames: true,
      enableDnsSupport: true
    });

    //--- Erroneous Line 
    //const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_12_3 });

    
    //simple fix : version 1
    //const engine = DatabaseInstanceEngine.postgres({ version: PostgresEngineVersion.VER_14_2 });

    //--------------------------
    //--- start : superior code fix , version 2
    const postgresFullVersion = process.env.POSTGRESMAJORVERSION ?? "14.2";
    const postgresMajorVersion = process.env.POSTGRESENGINEVERSION ?? "14";

    
    const engine = DatabaseInstanceEngine.postgres({
        version: PostgresEngineVersion.of(
          postgresFullVersion,
          postgresMajorVersion,
        ),
      });
    //--- end : superior code fix
    //--------------------------
    const dbInstance = new DatabaseInstance(this, 'Test_Postgres_CDK', {
      engine,
      allowMajorVersionUpgrade: true,
      vpc,
      iamAuthentication: true,
      instanceType: InstanceType.of(
        InstanceClass.BURSTABLE3,
        InstanceSize.MICRO
      ),
      databaseName: 'test_postgres'
    });

    new CfnOutput(this, 'dbEndpoint', {
      value: dbInstance.instanceEndpoint.hostname,
    });
  }
}
```