Search code examples
gitpostgresqljdbcproperties-filepostgresql-9.5

How to hide JDBC connection credentials from git?


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?


Solution

  • 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.