Having trouble getting psycopg2 to work with sql for a python bot

My current code is returning an error that seems related to psycopg2. If anyone might know, I’ve looked everywhere and can’t seem to find a solution to this

my code:from telegram.ext import (Updater, CommandHandler, ConversationHandler, MessageHandler, Filters, CallbackContext)
from telegram import KeyboardButton, ReplyKeyboardMarkup, Update
from data_source import DataSource
import os
import threading
import time
import datetime

ADD_REMINDER_TEXT = ‘add a reminder’
INTERVAL = 30

TOKEN = os.getenv(“TOKEN”)
ENTER_MESSAGE, ENTER_TIME = range(2)
datasource = DataSource(os.environ.get(“DATABASE_URL”))

def start_handler(update, context):
update.message.reply_text(“Hello, Creator!”, reply_markup=add_reminder_button())

def add_reminder_button():
keyboard = [[KeyboardButton(ADD_REMINDER_TEXT)]]
return ReplyKeyboardMarkup(keyboard)

def add_reminder_handler(update: Update, context: CallbackContext):
update.message.reply_text(“please enter a message of the reminder:”)
return ENTER_MESSAGE

def enter_message_handler(update: Update, context: CallbackContext):
update.message.reply_text(“please enter a time when bot should remind:”)
context.user_data[“message_text”] = update.message.text
return ENTER_TIME

def enter_time_handler(update: Update, context: CallbackContext):
message_text = context.user_data[“message_text”]
time = datetime.datetime.strptime(update.message.text, “%d/%m/%Y %H:%M”)
message_data = datasource.create_reminder(update.message.chat_id, message_text, time)
update.message.reply_text("your reminder: " + message_data.repr())
return CommandHandler.END

def start_check_reminders_task():
thread = threading.Thread(target=check_reminders, args=())
thread.daemon = True
thread.start()

def check_reminders():
while True:
for reminder_data in datasource.get_all_reminders():
if reminder_data.should_be_fired():
datasource.fire_reminder(reminder_data.reminder_id)
updater.bot.send_message(reminder_data.chat_id, reminder_data.message)
time.sleep(INTERVAL)

if name == ‘main’:
updater = Updater(TOKEN, use_context=True)
updater.dispatcher.add_handler(CommandHandler(“start”, start_handler))
conv_handler = ConversationHandler(
entry_points=[MessageHandler(Filters.regex(ADD_REMINDER_TEXT), add_reminder_handler)],
states={
ENTER_MESSAGE: [MessageHandler(Filters.all, enter_message_handler)],
ENTER_TIME: [MessageHandler(Filters.all, enter_time_handler)]
},
fallbacks=
)
updater.dispatcher.add_handler(conv_handler)
datasource.create_tables()
updater.start_polling()
start_check_reminders_task()

returns:
/Users/aao/.pyenv/versions/3.9.2/bin/python /Users/aao/Desktop/directory/documents/coding_macbook_air/pythonvenvsdk/main.py
invalid dsn: missing “=” after “udemy_telegram” in connection info string

Traceback (most recent call last):
File “/Users/aao/Desktop/directory/documents/coding_macbook_air/pythonvenvsdk/main.py”, line 64, in
datasource.create_tables()
File “/Users/aao/Desktop/directory/documents/coding_macbook_air/pythonvenvsdk/data_source.py”, line 53, in create_tables
raise error
File “/Users/aao/Desktop/directory/documents/coding_macbook_air/pythonvenvsdk/data_source.py”, line 45, in create_tables
conn = self.get_connection()
File “/Users/aao/Desktop/directory/documents/coding_macbook_air/pythonvenvsdk/data_source.py”, line 24, in get_connection
return psycopg2.connect(self.database_url, sslmode=‘allow’)
File “/Users/aao/.pyenv/versions/3.9.2/lib/python3.9/site-packages/psycopg2/init.py”, line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File “/Users/aao/.pyenv/versions/3.9.2/lib/python3.9/site-packages/psycopg2/extensions.py”, line 159, in make_dsn
tmp = parse_dsn(dsn)
psycopg2.ProgrammingError: invalid dsn: missing “=” after “udemy_telegram” in connection info string

is it possible psycopg2 is bugged? just a thought

To show the code you wrote with indentation, surround it with three backticks on a separate line before it and after it. Indentation is important in Python, so you want to retain it in your post.

Limiting your code to what is needed to reproduce the problem will greatly increase your chance to get a good answer. How to create a MCVE (Minimal, Complete, Verifiable Example):

From the error it is most likely that you made an error in creating or copying the connection info string that you get from an environment variable:

However, you do not show this. If you decide to show it, replace the password that is contained in it.

Of course that is possible, but it is much more likely you made a mistake.

user environment variables
oh I specified the database URL in environmental settings, but is this file required to be in the virtual environment database? its not currently located there just a thought

You put it in the right place.

Apparently the DATABASE_URL environment string is passed unaltered to psycopg2. That requires the connection info string in a different format, see The psycopg2 module content — Psycopg 2.9.9 documentation. I cannot check what the data_source.DataSource class does to it, I don’t have it and could not find it.

A few lines from the top is what you need to supply:
dbname=test user=postgres password=secret
Replace test with the database name, postgres with the user id and secret with the password you set when creating the database or the data you got from your DBA or instructor.

also is this an issue that telegram package is stuck on version 0.0.1??

dbname=udemy_telegram user=udemy_telegram_user password=password

Please post text as text, not as a screenshot.

Why do you bring this up? If that is the most recent version, that is the most recent version :man_shrugging: