Configuring a Postgresql connection with Play 2 and Slick-Play - database

I'm learning how to build an application using Scala and the Play 2 Framemork. I`ve created a new project using the activator tool, based on "play-scala-intro" current template.
The template have a sample app using the Play-Slick 1.0 for managing dependencies and is configured with a H2 DB, that worked without problems.
When I tried to change to a Postgres DB, I'm running in trouble. I get an error 500, telling me:
"Cannot connect to database [default]".
In the stack trace, the exception is:
"Configured Slick driver org.postgresql.Driver is not an instance of
requested profile slick.profile.BasicProfile"
So... What I already did:
I added to my build.sbt file the dependency:
"org.postgresql" % "postgresql" % "9.4-1201-jdbc41"
In my configuration file (application.conf), the DB connection is configured as:
slick.dbs.default.driver=org.postgresql.Driver
slick.dbs.default.db.url="jdbc:postgresql://localhost:5432/hello_play"
slick.dbs.default.db.user="postgres" slick.dbs.default.db.password=""
PS: I've tried with slick.dbs.default.driver="org.postgresql.Driver" too...
PS2: My db password is empty. I'm connecting with PgAdmin without problems

slick.dbs.default.driver must be a slick driver, not a JDBC driver. Your db config should look something like this:
slick.dbs.default.driver="slick.driver.PostgresDriver$"
slick.dbs.default.db.driver="org.postgresql.Driver"
slick.dbs.default.db.url="jdbc:postgresql://localhost:5432/hello_play"
slick.dbs.default.db.user="postgres"
slick.dbs.default.db.password=""

Related

Airflow 2.0 SQL Server connector

I want to use the airflow 2.01 docker-compose file from apaches github.
here is the link docker-compose.yaml and here is the link to the dockerfiledockerfile
I want to use a Dag which should grab data out of my SQL Server database. Actually I get the following error:
no module named pymssql
After I manually installed it, I get an error like no module named pyodbc.
When I want to install this manually I get an gcc error, that it is not possible to install.
Does anyone have any clue about this?
Is there any docker-compose file which is able to handle SQL Server connection for Airflow 2?
Thanks in advance

Connect to MSSQL from Spring boot application using windows authentication

I am currently using the below properties to connect to a remotely Mssql server from Java spring boot application:
spring.datasource.url=jdbc:sqlserver://ip\\domain;databaseName=name
spring.datasource.username=abc
spring.datasource.password=def
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
The application runs from both windows and unix servers. I need now to start using the windows authentication instead of the db credentials. This is already configured and supported from MSSQL side. From Java spring boot this is not working. I tried to add to the url: integratedSecurity=true it said it cannot find a dll. I added sqljdbc_auth.dll to java jre bin folder and it didnt work.
I'm working on MS SQL Server 2016, Hibernate 5.3.7.Final and Spring bot 2.2.0. For me, adding this line to properties worked: (without jtds!)
spring.datasource.url=jdbc:servername;databasename=your_db_name;integratedSecurity=true
You may also need these properties:
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
And if you encounter a problem with "no sqljdbc_auth in java.library.path".
You may refer to this answer no sqljdbc_auth.
Try use jtds driver, and in apllication properties use:
app.datasource.youraplication.jdbcUrl=jdbc:jtds:sqlserver://yourBdHost/yourSchemaBd;domain=yourDomain
Download the drivers from:
https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-2017
Add the jar to your project Right Click on project - > Build Path -> Configure Build Path - > Libraries -> Add External JARs
Add "integratedSecurity=true" to your connection string and remove username and password

Connecting Phoenix app to MS SQL Azure database

I am trying to connect a phoenix application to MS SQL. After looking around online I came across a couple of adapters called mssql_ecto & mssqlex.
I have added them to the project following the instructions in the readme, installed odbc and checked that the db is online but I am now getting the following error..
[error] Mssqlex.Protocol (#PID<0.13069.0>) failed to connect: ** (Mssqlex.Error) odbc_not_started
My app is configured as below..
config :my_app, MyApp.Repo,
adapter: MssqlEcto,
username: "<my_username>",
password: "<my_password>",
database: "test",
hostname: "<my_server>.database.windows.net",
pool_size: 10
My environment is as follows..
MacOS Sierra 10.12.5
Elixir version: 1.4.4
There is an issue in the mssql_ecto repo already, and I have tried the suggestions from there, but it still has not worked.
If anyone has managed to connect their phoenix/elixir application to MSSQL on macOS and could provide some instructions it would be greatly appreciated (even if it was done in a completely different way to my approach 😉 ).
Side note: Tried it in node with the same db and was able to connect to and query the db.
I've been working through the same issue this week, connecting to an Azure SQL Database, and this is what I've got working.
You are seeing the first of 2 main problems I ran into:
Erlang installed without ODBC
Mssqlex adapter is missing required configuration options
#1: Erlang installed without ODBC
If you've installed Elixir and Erlang using brew, or something like it, you are getting an installation of Erlang that does not include erlang-odbc. In order to remedy this, you will need to install Erlang from source.
I recommend that you install both Erlang and Elixir from source using the method documented here:
http://www.lambdacat.com/how-to-install-elixir-on-mac/
Just make sure that you remove the Kerl option, --without-odbc, before you build Erlang.
Now that you have Erlang installed correctly, the odbc_not_started error should go away... to be replaced by connection errors to the database.
#2: Mssqlex adapter is missing required configuration options
As of this moment, the mssqlex adapter does not support the following configuration options that Azure is looking for:
Encrypt
TrustServerCertificate
Connection Timeout
To get around this issue, I forked the mssqlex library and added those options in a branch. I am not committing to maintaining this so feel free to fork it yourself.
In order to use the modified mssqlex, replace the mix dependency with the github location and branch name:
{:mssqlex, git: "https://github.com/tvanhouten/mssqlex.git", branch: "azure-settings", override: true}
The override: true is important because mssql_ecto has a dependency on the mssqlex version in hex.
Now you can update your configuration:
config :my_app, MyApp.Repo,
adapter: MssqlEcto,
username: "<my_username>",
password: "<my_password>",
database: "test",
hostname: "<my_server>.database.windows.net",
pool_size: 10,
encrypt: "yes",
trust_server_certificate: "no",
connection_timeout: "30
Other "Gotchas" to Look Out For
Azure requires TCP connection:
In order to connect to an Azure SQL Database, you will need to make sure that the hostname: option starts with tcp:. So, all together, it will look like
hostname: "tcp:.database.windows.net"
Mssql_ecto does not support Ecto 2.2+:
If you're getting errors related to Ecto or Ecto.SubQuery, you probably need to make sure that you are using a version of Ecto that is supported by mssql_ecto. At this point in time, that means Ecto 2.1.x. Again, this requires a slight change to your mix dependencies:
{:ecto, ">= 2.1.0 and < 2.2.0"}
Dependency cache and lock issues:
If everything seems to be in order according to the above but it just doesn't seem like the new dependencies are being updated and compiled as expected, do the following:
Run mix deps.clean --all
Delete the mix.lock file from your root project directory
Run mix deps.get
Run mix deps.compile
That should do it!

Jhipster and Postgres connection

I'm trying to run the project in Jhipster based on this tutorial by the creator himself :https://www.youtube.com/watch?v=d1MEM8PdAzQ but it can't connect to Postgres
See errors below:
Caused by: org.postgresql.util.PSQLException: The server requested password-based authentication, but no password was provided.
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:473)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:203)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:146)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:35)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:47)
at org.postgresql.jdbc42.AbstractJdbc42Connection.<init>(AbstractJdbc42Connection.java:21)
at org.postgresql.jdbc42.Jdbc42Connection.<init>(Jdbc42Connection.java:28)
at org.postgresql.Driver.makeConnection(Driver.java:415)
at org.postgresql.Driver.connect(Driver.java:282)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:95)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:101)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:316)
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:518)
How do I connect Jhipster with postgreSQL? I am a newbie on jhipster
JHipster creates 3 configuration files: -
application.yml - main Spring Boot configuration file.
application-dev.yml
application-prod.yml
The application.yml file contains common properties, the other 2 hold specific properties to development and production environments.
If you look at application-dev.yml you'll see something like the following: -
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:postgresql://localhost:5432/myapp
username: myapp
password:
However, you still have to create your PostgreSQL database - the easiest way is via the pgAdmin tool but you can also create it via command line tools - a quick google will help you there!
If you don't want to work with docker compose file Version 3 in docker swarm mode, where it is best practice to use docker secrets, then you can create an .application.env-file and link it in your docker-compose.yml (Version 2) with 'env_file:'
$ cat .application.env
SPRING_PROFILES_ACTIVE=prod,swagger
SPRING_DATASOURCE_URL=jdbc:postgresql://postgresql:5432/database_name
SPRING_DATASOURCE_USER=database_user
SPRING_DATASOURCE_PASSWORD=database_password
JHIPSTER_SLEEP=10
[...]
At least I use it this way to keep the credentials away from my jhipster-projects, which are on Github, where I also want to put the *.yml-files.

ClassNotFoundException with Netbeans and H2 Database

So I followed the tutorial on the H2 Documentation page and used the "Connecting to a Database using JDBC" method of connecting to the database. I First added the h2-*.jar file to the Lib Folder (through Netbeans) and used the following to make the connection to my database I previously created.
Class.forName("org.h2.Driver");
connection = DriverManager.getConnection("jdbc:h2:~/" + DatabaseName);
This turned out to work in the IDE environment, however when I attempted to run the application directly from the jar executable I get the following error:
java.lang.ClassNotFoundException: org.h2.Driver ...
this error occurs at the Class.forName() class loader. So I did a little looking around and found that this was a prominent problem. One solution people use was to extract the class Loader from the current Thread as so:
Thread t = Thread.currentThread();
ClassLoader cl = t.getContextClassLoader();
cl.getClass().getClassLoader();
Class toRun = cl.loadClass("org.h2.Driver");
Unfortunately this seems to still result in the same error, so i'm wondering what i'm doing wrong. Should I be doing something to make sure the Driver is in the class path? I have no I idea how if that's the case.
Thanks!
You need to add the h2-*.jar file to the classpath when running the application, for example using
java -cp h2*.jar -jar yourApp.jar

Resources