2

I work on several small Java projects (using Maven+NetBeans) and store them in a public git repository.

I would like to use PostgreSQL JDBC in some of the projects, but don't want to make the connection credentials of my database public.

Surely there are other developers out there, who have already faced similar problem.

Please share your solutions.

It is probably possible to put the database name/user/password into a *.properties file and then ignore it in git repository, but keep it in the workarea? How to load that file in my Java projects?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • 1
    I usually go with the properties file approach as you described. There's plenty of resources out there describing how to load properties from a file. – Robby Cornelissen May 31 '16 at 09:43

3 Answers3

1

You can add that file to your .gitignore but keep it in the working copy just as you said, I don't really see the issue here. Just do

echo myprops.properties >> .gitignore

As far as loading these files goes, you can get more information here. They suggest using them like this:

Properties defaultProps = new Properties();
FileInputStream in = new FileInputStream("defaultProperties");
defaultProps.load(in);
in.close();

You can then access properties in your code using getProperty(String key) and getProperty(String key, String default)

Another thing I've seen people do is writing a git pre commit hook and using sed to replace the credentials in the files, but I don't have any experience with that.

dbt
  • 73
  • 1
  • 7
1

Another way than using properties file like @dbt said is by using environment variable.

So when you start the application, you use put the environment variable to the command. Usually application will have priority on loading the property file.

e.g. It will check properties file and load to all property. If environment variable exists, it will override the previous value.

Other people will have multiple property file that will be loaded by value of specific environment variable (e.g. staging, dev, production). This way you can keep multiple configuration for specific environments.

Related questions: What is the best way to manage configuration data

Community
  • 1
  • 1
dieend
  • 2,231
  • 1
  • 24
  • 29
1

Another alternative is to use ~/.pgpass to store the credentials.

Michael Sheaver
  • 2,059
  • 5
  • 25
  • 38
  • Will `~/.pgpass` really work when I run my Java program at command line? For example: [start_emb_jetty_mac.sh](https://github.com/afarber/jetty-newbie/blob/master/EmbWebsocketListener/start_emb_jetty_mac.sh). And what [JDBC connection parameters](https://jdbc.postgresql.org/documentation/head/connect.html) to use then? – Alexander Farber May 31 '16 at 12:40
  • I have not tried that. I do know that when I start 'psql' from the shell, it works. – Michael Sheaver May 31 '16 at 15:38
  • 1
    I've searched and [PostgreSQL JDBC driver](https://github.com/pgjdbc/pgjdbc) does not read `~/.pgpass` file – Alexander Farber May 31 '16 at 19:32