From 370d3014ebf95e0c422a3ea2b39d568d66eb326e Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Monteiro Date: Mon, 20 Oct 2025 21:06:44 -0300 Subject: Postgres Conteiner Settings --- compose.yaml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'compose.yaml') diff --git a/compose.yaml b/compose.yaml index db32d4c..21f3cea 100644 --- a/compose.yaml +++ b/compose.yaml @@ -30,5 +30,29 @@ services: depends_on: - mosquitto + + postgres: + image: postgres:13 + container_name: postgres + ports: + - 5432:5432 + environment: + POSTGRES_PASSWORD: public + POSTGRES_USER: root + POSTGRES_DB: control_station + volumes: + - $PWD/database/create-table.sql:/docker-entrypoint-initdb.d/init.sql + + + pgadmin: + image: dpage/pgadmin4 + container_name: pgadmin + environment: + - PGADMIN_DEFAULT_EMAIL=admin@admin.com + - PGADMIN_DEFAULT_PASSWORD=admin + ports: + - "5050:80" + depends_on: + - postgres networks: mosquitto: -- cgit v1.2.3 From ba5f30be9f69d18cbb3ffe2d00ba9cf287b77f43 Mon Sep 17 00:00:00 2001 From: Matheus Date: Tue, 21 Oct 2025 00:11:39 -0300 Subject: fix:criação automática de tabelas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 17 ++++++++++------- database/create_table.sql | 6 ------ database/init_scripts/create_table.sql | 6 ++++++ 3 files changed, 16 insertions(+), 13 deletions(-) delete mode 100644 database/create_table.sql create mode 100644 database/init_scripts/create_table.sql (limited to 'compose.yaml') diff --git a/compose.yaml b/compose.yaml index 21f3cea..c5102e4 100644 --- a/compose.yaml +++ b/compose.yaml @@ -29,21 +29,18 @@ services: - mosquitto depends_on: - mosquitto - - postgres: image: postgres:13 container_name: postgres - ports: - - 5432:5432 + networks: + - database environment: POSTGRES_PASSWORD: public POSTGRES_USER: root POSTGRES_DB: control_station volumes: - - $PWD/database/create-table.sql:/docker-entrypoint-initdb.d/init.sql - - + - ${PWD}/database/init_scripts:/docker-entrypoint-initdb.d + - db_data:/var/lib/postgresql/data pgadmin: image: dpage/pgadmin4 container_name: pgadmin @@ -52,7 +49,13 @@ services: - PGADMIN_DEFAULT_PASSWORD=admin ports: - "5050:80" + networks: + - database depends_on: - postgres networks: mosquitto: + database: + +volumes: + db_data: diff --git a/database/create_table.sql b/database/create_table.sql deleted file mode 100644 index 8ee801a..0000000 --- a/database/create_table.sql +++ /dev/null @@ -1,6 +0,0 @@ -create table if not exists climameasures ( - stationname varchar(255), - measurevalue int null, - unit float null, - currenttime timestamp default current_timestamp -); \ No newline at end of file diff --git a/database/init_scripts/create_table.sql b/database/init_scripts/create_table.sql new file mode 100644 index 0000000..8ee801a --- /dev/null +++ b/database/init_scripts/create_table.sql @@ -0,0 +1,6 @@ +create table if not exists climameasures ( + stationname varchar(255), + measurevalue int null, + unit float null, + currenttime timestamp default current_timestamp +); \ No newline at end of file -- cgit v1.2.3 From 52a4fe2625b63683fcdcd74f704e49aa65cb0dd8 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Monteiro Date: Sun, 26 Oct 2025 16:09:12 -0300 Subject: database connection tested successfully - tables readed on pgadmin --- compose.yaml | 11 ++++++++--- database/init_scripts/create_table.sql | 21 --------------------- mqtt_client/database.py | 10 ++++++++++ mqtt_client/main.py | 13 ++++++++++--- mqtt_client/requirements.txt | 4 +++- 5 files changed, 31 insertions(+), 28 deletions(-) delete mode 100644 database/init_scripts/create_table.sql (limited to 'compose.yaml') diff --git a/compose.yaml b/compose.yaml index c5102e4..549379a 100644 --- a/compose.yaml +++ b/compose.yaml @@ -23,6 +23,7 @@ services: environment: MQTT_CLIENT_USER: ${MQTT_CLIENT_USER} MQTT_CLIENT_PASSWORD: ${MQTT_CLIENT_PASSWORD} + DATABASE_URL: postgresql://root:public@postgres:5432/control_station build: context: ./mqtt_client/ networks: @@ -33,14 +34,18 @@ services: image: postgres:13 container_name: postgres networks: - - database + - mosquitto + ports: + - "5432:5432" environment: POSTGRES_PASSWORD: public POSTGRES_USER: root POSTGRES_DB: control_station volumes: - - ${PWD}/database/init_scripts:/docker-entrypoint-initdb.d + #- ${PWD}/database/init_scripts:/docker-entrypoint-initdb.d - db_data:/var/lib/postgresql/data + + pgadmin: image: dpage/pgadmin4 container_name: pgadmin @@ -50,7 +55,7 @@ services: ports: - "5050:80" networks: - - database + - mosquitto depends_on: - postgres networks: diff --git a/database/init_scripts/create_table.sql b/database/init_scripts/create_table.sql deleted file mode 100644 index ae5b3d1..0000000 --- a/database/init_scripts/create_table.sql +++ /dev/null @@ -1,21 +0,0 @@ -create table if not exists station ( - id integer, - name varchar(100), - locationid int null, - installation_date date -); - -create table if not exists climameasures ( - stationname varchar(255), - measurevalue int null, - unit float null, - currenttime timestamp default current_timestamp -); - - -create table if not exists climameasures ( - stationname varchar(255), - measurevalue int null, - unit float null, - currenttime timestamp default current_timestamp -); \ No newline at end of file diff --git a/mqtt_client/database.py b/mqtt_client/database.py index e69de29..c0691a1 100644 --- a/mqtt_client/database.py +++ b/mqtt_client/database.py @@ -0,0 +1,10 @@ +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker +import os + +DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://root:public@postgres:5432/control_station") + +engine = create_engine(DATABASE_URL) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) +Base = declarative_base() diff --git a/mqtt_client/main.py b/mqtt_client/main.py index dad09b5..109ca49 100644 --- a/mqtt_client/main.py +++ b/mqtt_client/main.py @@ -2,8 +2,15 @@ import paho.mqtt.client as mqtt from paho.mqtt.enums import MQTTProtocolVersion import json from time import sleep +from database import engine +from models import Base import os +print("Creating ORM SQL Tables..") +Base.metadata.create_all(bind=engine) +print("Tables created successfully.") + + def on_connect(client, userdata, flags, reason_code, properties): print(f"Conectado: {reason_code}") # Me inscrevo em todos os tópicos sobre clima @@ -13,9 +20,9 @@ def on_connect(client, userdata, flags, reason_code, properties): def on_message(client, userdata, msg): payload = json.loads(msg.payload) print(msg.topic) - print(f"Value: {payload["value"]}") - print(f"Unit: {payload["unit"]}") - print(f"Timestamp: {payload["timestamp"]}") + print(f"Value: {payload['value']}") + print(f"Unit: {payload['unit']}") + print(f"Timestamp: {payload['timestamp']}") try: user_name = os.environ["MQTT_CLIENT_USER"] diff --git a/mqtt_client/requirements.txt b/mqtt_client/requirements.txt index 96a2716..1521b2d 100644 --- a/mqtt_client/requirements.txt +++ b/mqtt_client/requirements.txt @@ -1 +1,3 @@ -paho-mqtt==2.1.0 +sqlalchemy +psycopg2-binary +paho-mqtt -- cgit v1.2.3