Skip to content

Commit 2039b13

Browse files
authored
Destination PostgreSQL: add schema name, use Quix Streams version (#612)
* add new env var, remove embedded sink * TEMPORARY: install from repo branch * fix optional install * remove redundant requirement * add postgres as a deployment option * fix dockerfile * update default postgres values for easy quix deployment connecting
1 parent 1325aa8 commit 2039b13

File tree

9 files changed

+136
-208
lines changed

9 files changed

+136
-208
lines changed

docker/postgres/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# PostgreSQL
2+
3+
This sample demonstrates how to deploy and use a PostgreSQL database in your Quix Cloud pipeline.
4+
5+
Please note: this image is provided by Postgres and is offered as-is, with no specific support from Quix. For any support, contact Postgres directly.
6+
7+
## How to Run
8+
9+
1. Create an account or log in to your [Quix](https://portal.platform.quix.io/signup?xlink=github) account and navigate to the Code Samples section.
10+
2. Click `Deploy` to launch a pre-built container in Quix.
11+
3. Enable state, otherwise changes will be lost on restart. Please note, the necessary storage type may not be supported on all Quix Platforms.
12+
13+
## How to Use
14+
To interact with PosgreSQL from your pipeline, either use one of the no-code [PostgreSQL connectors](https://quix.io/integrations?category=SQL+DB)
15+
in Quix Cloud or use a Quix Streams [PostgreSQLSink](https://quix.io/docs/quix-streams/connectors/sinks/postgresql-sink.html) directly.
16+
17+
Use the following values to connect:
18+
```shell
19+
host="postgresql"
20+
port=80
21+
username="admin" # or your POSTGRES_USER value
22+
password="<YOUR PASSWORD>" # your POSTGRES_PASSWORD value
23+
db="quix" # or your POSTGRES_DB value
24+
```
25+
26+
## Contribute
27+
28+
Feel free to fork this project on the [GitHub](https://github.com/quixio/quix-samples) repository and contribute your enhancements. Any accepted contributions will be attributed accordingly.
29+
30+
## License & Support
31+
32+
This project is open source under the Apache 2.0 license and available in our [GitHub](https://github.com/quixio/quix-samples) repo. Remember, this image is provided by Influx and is offered as-is, with no InfluxDB specific support from Quix.

docker/postgres/dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM postgres:16
2+
3+
# Set environment variables for PostgreSQL (will be overridden in K8s runtime if needed)
4+
ENV PGDATA=/app/state/pgdata
5+
ENV POSTGRES_USER=admin
6+
ENV POSTGRES_DB=quix
7+
8+
# Create non-root user (if not using built-in `postgres` user)
9+
# We'll use the default `postgres` user that already exists in the base image.
10+
11+
USER root
12+
13+
# Create the data directory and set permissions
14+
RUN mkdir -p "$PGDATA" \
15+
&& chown -R postgres:postgres /app/state \
16+
&& chmod -R 700 /app/state
17+
18+
USER postgres
19+
20+
EXPOSE 5432
21+
22+
CMD ["postgres", "-c", "listen_addresses=*"]

docker/postgres/icon.png

7.27 KB
Loading

docker/postgres/library.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"libraryItemId": "postgresql",
3+
"name": "postgreSQL",
4+
"language": "docker",
5+
"tags": {
6+
"Category": ["Database"],
7+
"Type": ["Auxiliary Services"]
8+
},
9+
"shortDescription": "Run postgreSQL in your pipeline.",
10+
"DefaultFile": "dockerfile",
11+
"EntryPoint": "dockerfile",
12+
"IconFile": "icon.png",
13+
"DeploySettings": {
14+
"DeploymentType": "Service",
15+
"CpuMillicores": 1000,
16+
"MemoryInMb": 8000,
17+
"Replicas": 1,
18+
"Network": {
19+
"ServiceName": "postgresql",
20+
"Ports":
21+
[
22+
{
23+
"Port": 80,
24+
"TargetPort": 5432
25+
}
26+
]
27+
}
28+
},
29+
"Variables": [
30+
{
31+
"Name": "POSTGRES_USER",
32+
"Type": "EnvironmentVariable",
33+
"InputType": "FreeText",
34+
"Description": "The root username to initialize PostgreSQL with",
35+
"DefaultValue": "admin",
36+
"Required": true
37+
},
38+
{
39+
"Name": "POSTGRES_PASSWORD",
40+
"Type": "EnvironmentVariable",
41+
"InputType": "Secret",
42+
"Description": "The root password to initialize MongoDB with",
43+
"Required": true
44+
},
45+
{
46+
"Name": "POSTGRES_DB",
47+
"Type": "EnvironmentVariable",
48+
"InputType": "FreeText",
49+
"Description": "The default database name to initialize PostgreSQL with",
50+
"DefaultValue": "quix",
51+
"Required": true
52+
}
53+
]
54+
}

python/destinations/postgres/dockerfile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
FROM python:3.12.5-slim-bookworm
2-
2+
3+
# TODO: remove this RUN block when done doing "@ git+" install in requirements.txt
4+
# This should be done BEFORE merging PR
5+
RUN apt-get update && \
6+
apt-get install -y git && \
7+
apt-get clean && \
8+
rm -rf /var/lib/apt/lists/*
9+
310
# Set environment variables for non-interactive setup and unbuffered output
411
ENV DEBIAN_FRONTEND=noninteractive \
512
PYTHONUNBUFFERED=1 \

python/destinations/postgres/library.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,31 @@
2626
"Type": "EnvironmentVariable",
2727
"InputType": "FreeText",
2828
"Description": "Host address for the PostgreSQL instance.",
29-
"DefaultValue": "localhost",
29+
"DefaultValue": "postgresql",
3030
"Required": true
3131
},
3232
{
3333
"Name": "POSTGRES_PORT",
3434
"Type": "EnvironmentVariable",
3535
"InputType": "FreeText",
3636
"Description": "Port number for the PostgreSQL instance.",
37-
"DefaultValue": "5432",
37+
"DefaultValue": 80,
3838
"Required": true
3939
},
4040
{
4141
"Name": "POSTGRES_DBNAME",
4242
"Type": "EnvironmentVariable",
4343
"InputType": "FreeText",
4444
"Description": "Database name in PostgreSQL where data should be stored.",
45-
"DefaultValue": "mydatabase",
45+
"DefaultValue": "quix",
4646
"Required": true
4747
},
4848
{
4949
"Name": "POSTGRES_USER",
5050
"Type": "EnvironmentVariable",
5151
"InputType": "FreeText",
5252
"Description": "Username for the PostgreSQL database.",
53-
"DefaultValue": "myuser",
53+
"DefaultValue": "admin",
5454
"Required": true
5555
},
5656
{
@@ -65,9 +65,16 @@
6565
"Type": "EnvironmentVariable",
6666
"InputType": "FreeText",
6767
"Description": "The PostgreSQL table where data will be stored. If the table does not exist, it will be created automatically.",
68-
"DefaultValue": "numbers",
6968
"Required": true
7069
},
70+
{
71+
"Name": "POSTGRES_SCHEMA",
72+
"Type": "EnvironmentVariable",
73+
"InputType": "FreeText",
74+
"Description": "The schema name. Schemas are a way of organizing tables and not related to the table data, referenced as `<schema_name>.<table_name>`. PostrgeSQL uses 'public' by default under the hood.",
75+
"DefaultValue": "public",
76+
"Required": false
77+
},
7178
{
7279
"Name": "SCHEMA_AUTO_UPDATE",
7380
"Type": "EnvironmentVariable",

python/destinations/postgres/main.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
import os
22
from quixstreams import Application
3-
from postgres_sink import PostgresSink
4-
3+
from quixstreams.sinks.community.postgresql import PostgreSQLSink
54
# Load environment variables from a .env file for local development
65
from dotenv import load_dotenv
76
load_dotenv()
87

98
# Initialize PostgreSQL Sink
10-
postgres_sink = PostgresSink(
9+
postgres_sink = PostgreSQLSink(
1110
host=os.environ["POSTGRES_HOST"],
1211
port=int(os.environ["POSTGRES_PORT"]),
1312
dbname=os.environ["POSTGRES_DBNAME"],
1413
user=os.environ["POSTGRES_USER"],
1514
password=os.environ["POSTGRES_PASSWORD"],
1615
table_name=os.environ["POSTGRES_TABLE"],
17-
schema_auto_update=os.environ.get("SCHEMA_AUTO_UPDATE", "true").lower() == "true"
16+
schema_name=os.getenv("POSTGRES_SCHEMA", "public"),
17+
schema_auto_update=os.environ.get("SCHEMA_AUTO_UPDATE", "true").lower() == "true",
1818
)
1919

20-
# Buffer settings
21-
buffer_size = int(os.environ.get("BATCH_SIZE", "1000"))
22-
buffer_delay = float(os.environ.get("BATCH_TIMEOUT", "1"))
23-
2420
# Initialize the application
2521
app = Application(
2622
consumer_group=os.environ["CONSUMER_GROUP_NAME"],
2723
auto_offset_reset="earliest",
28-
commit_interval=buffer_delay,
29-
commit_every=buffer_size
24+
commit_interval=float(os.environ.get("BATCH_TIMEOUT", "1")),
25+
commit_every=int(os.environ.get("BATCH_SIZE", "1000"))
3026
)
3127

3228
# Define the input topic

0 commit comments

Comments
 (0)