Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is this package usable for normal runtime (not test)? #292

Open
VTrngNghia opened this issue Dec 4, 2024 · 3 comments
Open

Is this package usable for normal runtime (not test)? #292

VTrngNghia opened this issue Dec 4, 2024 · 3 comments
Labels
status: waiting-for-feedback We need additional information before we can continue

Comments

@VTrngNghia
Copy link

VTrngNghia commented Dec 4, 2024

For my service use case, I need to load the database from a PostGre .sql dump file onto memory (I'll try Flyway for that), extract some of the data, then discard the on-memory database. This is on a cron job, to be done weekly. I will do this on normal runtime, not just test time.

Is that possible to setup with this package?

I'm trying this config

@AutoConfigureEmbeddedDatabase(
	beanName = "postgresDataSource", provider = EMBEDDED, type = POSTGRES)
public class PostgresConfig {
	@Autowired
	private DataSource postgresDataSource;
}

But it's throwing error:


Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (the profiles local are currently active).

Note: I have multiple datasources & database types for my app (mongo, mysql,...) so the driver will probably need to be manually declared. The problem is, I don't know how...

@tomix26
Copy link
Collaborator

tomix26 commented Dec 4, 2024

Hi @VTrngNghia, thanks for your question. This project is tightly focused on integration with the Spring Test module, so I don't recommend using it in production, and I don't think it makes sense to do so.

As an alternative, you might consider https://github.com/zonkyio/embedded-postgres, where you can easily create a postgres database using its internal API, see the example below. However, keep in mind that this is also a library primarily intended for testing. This means test dependencies like JUnit will be included in your project.. Such a decision needs to be carefully considered.

EmbeddedPostgres.Builder builder = EmbeddedPostgres.builder();
EmbeddedPostgres pg = builder.start()
DataSource dataSource = pg.getPostgresDatabase();

@tomix26 tomix26 added the status: waiting-for-feedback We need additional information before we can continue label Dec 4, 2024
@VTrngNghia
Copy link
Author

Thanks @tomix26. The embedded-postgres seems to work.

	@Bean
	DataSource postgresDataSource() throws IOException {
		return EmbeddedPostgres.builder()
			.setPort(5433)
			.start().getPostgresDatabase();
	}

I applied the flyway migration to it.

{"@timestamp":"2024-12-05T07:51:54.326331379+07:00","@version":"1","message":"Successfully applied 1 migration to schema \"public\", now at version v1 (execution time 00:00.047s)","logger_name":"org.flywaydb.core.internal.command.DbMigrate","thread_name":"reactor-http-epoll-2","level":"INFO","level_value":20000}

I see it's exposed via port 5433 successfully.

08:15:45.677 [main] INFO org.flywaydb.core.internal.database.base.BaseDatabaseType -- Database: jdbc:postgresql://localhost:5433/postgres (PostgreSQL 14.15)

But attempts to connect to it from an external tool (for debugging) fails: [28000] FATAL: role "root" does not exist.
How can I configure credentials to this embedded postgres?

I found the default username & password: postgres and postgres. This will work for now.
But do you know how I can configure DB name, username and password?

@tomix26
Copy link
Collaborator

tomix26 commented Dec 6, 2024

The default postgres user cannot be changed. However, through the postgres database, you can create new users and databases using SQL commands. You can then connect to these databases using the EmbeddedPostgres#getDatabase(String userName, String dbName) method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue
Projects
None yet
Development

No branches or pull requests

2 participants