Installation sudo apt-get install postgresql
Installing the GUI interface sudo apt-get install pgadmin3
Starting psql on command line: sudo -u postgres psql postgres
username dbname
postgres is the user/admin which is there by default.
Also you can this on the command line to start psql: psql -u postgres -h localhost
Creating a new db sudo -u postgres createdb mydb1 or you can use the GUI to add a new db.
Starting psql for your db sudo -u postgres psql mydb1
Time to configure postgresql to your Spring application, we assume that you are using Maven as the build tool.
Add the following dependency in your pom.xml
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
But before this make sure you check the version of your postgres by writing on the command line
psql --version, the above code is valid if your version is 9.1.4 else go here http://mvnrepository.com/artifact/postgresql/postgresql/ and add the dependency to your pom.xml accordingly.
Checking that your code is working by creating a connection to the Database. If the following code does not give any error then you have a reason to celebrate. Make sure to add the try and catch blocks wherever necessary.
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/mydb1", "postgres",
"crossword");
Now we need to add a Configuration file in our SpringFramework that will have the configuration for the datasource to connect to postgres. Your configuration will have the following datasource:
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/mydb1");
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUsername("postgres");
dataSource.setPassword("crossword");
SimpleJdbcTemplate db = new SimpleJdbcTemplate(dataSource);
After making these changes this is the commit:
https://github.com/gauravmunjal/HelloWorldSpring/commit/51041b61b7230816175e53fe47b78132fd762f39
After making these changes this is the commit:
https://github.com/gauravmunjal/HelloWorldSpring/commit/51041b61b7230816175e53fe47b78132fd762f39
References
No comments:
Post a Comment