Spring and Foreman don’t always see eye to eye, and if you spin up a new rails 5 app rails new my_new_app then by default you will get Spring installed.
Having Spring installed is a good thing as it helps speed up development, if you don’t fully understand what Spring and Foreman are doing though, you end up spending some time scratching your head.
When you add Foreman to your project with gem install foreman certain things will work (foreman start worked perfectly for me) but some things like foreman run rails g... will shout about ENVs not being set that you know for a fact are set.
I realised that this is likely because Spring is running and doesn’t have access to the ENV variables that you set since before starting the project.
spring stop then trying my Foreman command again worked perfectly for me.
Here is a some annotated output of what was happening in my case, hopefully it helps you;