8

Using the node pg package, pg, I'm trying to connect to a PostgreSQL DB created in AWS-RDS. I believe the DB was NOT given a name when creating an instance, note that an instance is different than a DB. When trying to connect using Client or Pool from pg my syntax is

const client = new Client({
  host     : <<RDS ENDPOINT>>,
  user     : <<RDS DB USERNAME>>,
  password : <<RDS DB PASSWORD>>,
  port     : <<RDS DB PORT>>
});
client.connect()
  .then(data => {
    console.log('connected');
  })
  .catch(err => {
    console.log(err);
  })

But every time I am returned with error: database <<linux user name>> does not exist.

Now creating a different PostgreSQL instance and supplying a name for the DB I am able to add a database prop to my Client objects config and everything works and I am returned with a console log of connected.

So my question is, how am I supposed to connect to the DB on AWS-RDS without supplying a database prop in my Client config?

Edits

edit 1

Supplying a database prop with an empty string will be overwritten with my linux username

Brandon Benefield
  • 1,504
  • 1
  • 21
  • 36
  • 1
    When you create a Postgres DB it says under `Additional configuration > Database options > Initial database name`: *"If you do not specify a database name, Amazon RDS does not create a database."* Some clients may not be able to connect without a database. – j08lue Aug 16 '19 at 08:56

1 Answers1

14

With the node-postgres package you need to supply a database prop in your Client/Pool config object. If your PostgreSQL DB does not have a name, say if you created it using AWS-RDS then this DB NAME will default to postgres. Simply supplying the database prop with postgres should solve any problems you have with an un-named DB.

const client = new Client({
  host     : <<RDS ENDPOINT>>,
  user     : <<RDS DB USERNAME>>,
  password : <<RDS DB PASSWORD>>,
  port     : <<RDS DB PORT>>,
  database : 'postgres' // supplying a database name with `postgres`

});
Brandon Benefield
  • 1,504
  • 1
  • 21
  • 36
  • Took me a long time to figure out. It was also mentioned here: https://stackoverflow.com/questions/43099155/how-can-i-change-the-database-name-in-aws-rds-for-postgresql but could not locate in the official documentation. – dz902 Nov 06 '19 at 07:50