I am not sure if I understand your actual problem. If your actual problem is how to create a table for that and what field types to use, then you need to take a look in the Data Types chapter of the PostgreSQL documentation.
To create a table in PostgreSQL, you can go ahead with the CREATE TABLE
statement.
For the String
key you probably want to use varchar(n)
datatype. First determine the maximum length based on the existing keys, choose a reasonable maximum length to use in the DB and document it so that future additions won't use a key longer than that. Let's assume that 100
is a reasonable maximum length.
For the byte[]
value you need the bytea
datatype. We will also assume that this is never null, so use the NOT NULL
here.
Now, the final CREATE
statement would look like:
CREATE TABLE tablename (
key VARCHAR(100) PRIMARY KEY,
value BYTEA NOT NULL
);
You can eventually add an autogenerated technical ID of serial
datatype as primary key so that data management is more efficient. You only need to make the key
NOT NULL
and UNIQUE
.
CREATE TABLE tablename (
id SERIAL PRIMARY KEY,
key VARCHAR(100) NOT NULL UNIQUE,
value BYTEA NOT NULL
);
In the Java/JDBC side you could make use of ResultSet#getBytes()
, or more efficiently ResultSet#getBinaryStream()
to get the binary value associated with the key.
That said, storing binary data in a database put big question marks around the design. They are in no way searchable and if it involves character data, then you might need to take the character encoding precisely into account in the program which is used to insert and select the data.