0

I have a bash file setting up a postgres-database.

#configure_db.sh
source ./secrets/sqlpassword.sh
createdb -U myuser mydatabase

I have a simple bash file exporting the password

#secrets/sqlpassword.sh
export PGPASSWORD='mypassword'

The rest of the node server uses environment variables saved in a .env-file. For the sake of order and simplicity i would want my postgres password stored in the same file:

//.env
postgrespassword='mypassword'

How can you import a variable from a .env-file to as bash-file?

Is there another way of solving the above?

Simon
  • 621
  • 4
  • 21

1 Answers1

1

Why don't you follow the same pattern?

Change

#configure_db.sh
source ./secrets/sqlpassword.sh
createdb -U myuser mydatabase

to

#configure_db.sh
PGPASSWORD=$(grep postgrespassword /path/to/.env | cut -d "=" -f2)
createdb -U myuser mydatabase
Tolis Gerodimos
  • 3,782
  • 2
  • 7
  • 14
  • First thing the variable has to be named PGPASSWORD, as that is the environment variable that postgres will use. However, even with a correct name, when sourcing .env the variables aren't just read and exported. As commented by tripleee you probably need some kind of parser. – Simon Feb 06 '22 at 14:26
  • Excuse me i missunderstood. Maybe this is what you need? – Tolis Gerodimos Feb 06 '22 at 17:17