hapifhir / hapi-fhir-jpaserver-starter

Apache License 2.0
394 stars 1.05k forks source link

HAPI doesn't connect to Postgres Database #585

Closed Rakaniyu closed 1 year ago

Rakaniyu commented 1 year ago

Hi there, i've already make all changes needed to connect HAPI Server to a postgres database but when I connect to database, I don't see the tables from HAPI.

XcrigX commented 1 year ago

HAPI will auto-create the tables when it starts up the first time, assuming it is configured correctly and has the proper permissions. I'd expect you'd see errors in the logs if it failed to connect or create the tables.

You might need to uncomment this line in the application.yaml to have it do so: https://github.com/hapifhir/hapi-fhir-jpaserver-starter/blob/master/src/main/resources/application.yaml#L36

Rakaniyu commented 1 year ago

I just uncomment it but it still doesn't create the tables, if I connect through PgAdmin 4 to the database, it doesn't seem like it's connected in database activity (I don't know if it should appear but I'm guessing that)

XcrigX commented 1 year ago

If the server comes up and "works" - and you don't see errors in the log file. I would suspect that it is then spinning up the in-memory H2 database to connect to, rather than your external Postgres. Did you specify the DB connection properties here: ?
https://github.com/hapifhir/hapi-fhir-jpaserver-starter/blob/master/src/main/resources/application.yaml#L16

Rakaniyu commented 1 year ago

Yes, I modified like this

spring: main: allow-circular-references: true

allow-bean-definition-overriding: true

flyway: enabled: false check-location: false baselineOnMigrate: true datasource: url: 'jdbc:postgresql://localhost:5432/hapi'

url: jdbc:h2:mem:test_mem

username: db_user
password: db_pwd
driverClassName: org.postgresql.Driver
max-active: 15

# database connection pool size
hikari:
  maximum-pool-size: 10

jpa: properties: hibernate.format_sql: false hibernate.show_sql: false

  #Hibernate dialect is automatically detected except Postgres and H2.
  #If using H2, then supply the value of ca.uhn.fhir.jpa.model.dialect.HapiFhirH2Dialect
  #If using postgres, then supply the value of ca.uhn.fhir.jpa.model.dialect.HapiFhirPostgres94Dialect
  hibernate.dialect: ca.uhn.fhir.jpa.model.dialect.HapiFhirPostgres94Dialect
     hibernate.hbm2ddl.auto: update

hibernate.jdbc.batch_size: 20

hibernate.cache.use_query_cache: false

hibernate.cache.use_second_level_cache: false

hibernate.cache.use_structured_entries: false

hibernate.cache.use_minimal_puts: false

These settings will enable fulltext search with lucene or elastic

  hibernate.search.enabled: false

lucene parameters

hibernate.search.backend.type: lucene

hibernate.search.backend.analysis.configurer: ca.uhn.fhir.jpa.search.HapiHSearchAnalysisConfigurers$HapiLuceneAnalysisConfigurer

hibernate.search.backend.directory.type: local-filesystem

hibernate.search.backend.directory.root: target/lucenefiles

hibernate.search.backend.lucene_version: lucene_current

elastic parameters ===> see also elasticsearch section below <===

hibernate.search.backend.type: elasticsearch

hibernate.search.backend.analysis.configurer: ca.uhn.fhir.jpa.search.HapiHSearchAnalysisConfigurers$HapiElasticAnalysisConfigurer

Rakaniyu commented 1 year ago

Sorry it's copied horrible, i attach the file in txt format

application.txt

XcrigX commented 1 year ago

Looks correct. Can you reach the server when it comes up? Like can you search /Patient ? Anything in the logs?

My guess at this point would be that it is not picking up your application.yaml file. How are you packaging it? How are you running the server?

Rakaniyu commented 1 year ago

The server works perfectly and I can POST the fhir resources.

For running the server, I clon this repository and then run docker-compose up

XcrigX commented 1 year ago

The Docker compose includes a Postgres image. So I suspect that HAPI is connecting to that postgres instance within the container rather than your external instance running on your own localhost. (Localhost to the docker container would be the docker container itself, not your computer, I think) .

Rakaniyu commented 1 year ago

Yes I know and i'm connecting to that Postgres database. That's why I use localhost in application.yaml but HAPI is connecting to I don't know where

XcrigX commented 1 year ago

I'm mostly out of ideas. You could:

That's about all I can think to try.

Rakaniyu commented 1 year ago

Yes i've already tried that, I'm convinced that is HAPI connecting to another internal database or something like that.

Anyways, thanks you so much for your time

Rakaniyu commented 1 year ago

I've just resolved it!

The problem was in application.yaml in

datasource:
    url: 'jdbc:postgresql://localhost:5432/fhir'

localhost must be changed with postgres container name for example

datasource:
    url: 'jdbc:postgresql://hapi-fhir-postgres:5432/fhir'