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
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");
References