Goal: Get started with PostgreSQL and Docker in 30'
Prerequisite: Running Scout "Hello World" + running Docker installation, some shell (eg. bash as used in the text below)
Steps to reproduce:
- Pull PostgreSQL Docker image
- Create Database
- Add PostgreSql Service to Scout "Hello World"
- Use the PostgreSql service
Disclaimer: This is copy/paste from the thread to connect to MySQL. it only differs in the places that are specific to PostgreSQL.
1. Pull PostgreSQL Docker Image
Command line sequence to pull PostgreSQL image and start PostgreSQL container
docker docker pull postges:latest
docker run --name postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:latest
After the above commands you have an instance of a PostgreSQL server running in a Docker container.
2. Create Database
Use the running container to add a minimal PostgreSQL datbase named 'my_first_db'
docker exec -it postgres bash
Now start and use the PostgreSQL command line interface (CLI)
psql -U postgres
create database my_first_db;
\c my_first_db
CREATE TABLE example (id SERIAL PRIMARY KEY, value VARCHAR(100), time_created TIMESTAMP(6));
INSERT INTO example (value, time_created) VALUES ('hello world!', current_timestamp);
\q
4. Add PostgreSqlService Service to Scout "Hello World"
There are two points for this step. First, add the necessary Maven dependencies to the pom.xml of the Scout "Hello World" server module.
<!-- add the two dependencies below -->
<dependency>
<groupId>org.eclipse.scout.rt</groupId>
<artifactId>org.eclipse.scout.rt.server.jdbc</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
</project>
Most, likely you have now to update the helloworld.server, the helloworld.server.app.dev and the helloworld.server.app.war projekcts (use Alt+F5 or the corresponding context menu in the Package Explorer)
Then, add the PostgreSqlServiceto the hello world server according to the code shown below:
public class PostgreSqlService extends AbstractPostgreSqlService {
@Override
protected String getConfiguredJdbcMappingName() {
String host = "<Your Docker IP here>"; // Docker IP as obtained from 'docker-machine ip default'
return "jdbc:postgresql://" + host + "/my_first_db";
}
@Override
protected String getConfiguredUsername() {
return "postgres";
}
@Override
protected String getConfiguredPassword() {
return "password";
}
}
5. Use the PostgreSqlService Service
This is the simplest step: Just use the new Service in the existing HelloWorldService and restart the hello world server and client app in the Eclipse IDE.
For example:
public class HelloWorldService implements IHelloWorldService {
@Override
public HelloWorldFormData load(HelloWorldFormData input) {
StringHolder text = new StringHolder();
SQL.selectInto("SELECT value FROM example WHERE id = 1 INTO :text", new NVPair("text", text));
StringBuilder msg = new StringBuilder();
msg.append(text.getValue());
input.getMessage().setValue(msg.toString());
return input;
}
}
When the hello world form is opened in the message "hello world!" from the PostgreSQL database is shown in the message field.