Getting psycopg.ProgrammingError: invalid connection option "database" when pasing database parameter - python-3.x

Hey I am new to Databases and I decided to use Postgresql for convinience. And I am using an adapter for the Python programming language of the database named Psycopg I followed the installation tutorial of Psycopg2 but I was getting an error so I decided to install psycopg3 and it installed successfully! but when I pass the database parametere I get the following error:
Traceback (most recent call last):
File "C:\Users\Aditya\AppData\Local\Programs\Python\Python310\lib\site-packages\psycopg\conninfo.py", line 97, in _parse_conninfo
return pq.Conninfo.parse(conninfo.encode())
File "psycopg_binary\\pq/conninfo.pyx", line 30, in psycopg_binary.pq.Conninfo.parse
psycopg.OperationalError: invalid connection option "database"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\Aditya\Desktop\Aditya\TGbot\dbhelper.py", line 3, in <module>
conn = psycopg.connect(
File "C:\Users\Aditya\AppData\Local\Programs\Python\Python310\lib\site-packages\psycopg\connection.py", line 561, in connect
conninfo = make_conninfo(**params)
File "C:\Users\Aditya\AppData\Local\Programs\Python\Python310\lib\site-packages\psycopg\conninfo.py", line 56, in make_conninfo
_parse_conninfo(conninfo)
File "C:\Users\Aditya\AppData\Local\Programs\Python\Python310\lib\site-packages\psycopg\conninfo.py", line 99, in _parse_conninfo
raise e.ProgrammingError(str(ex))
psycopg.ProgrammingError: invalid connection option "database"
But when i didnt pass the database argument i didnt get any kind of error... here's my code:
import psycopg
conn = psycopg.connect(
host="localhost",
database="suppliers",
user="postgres",
password="pas")
What i am doing wrong here? I am sure that the i have created the database with that name and the password is also correct.

You look to be using psycopg3. database was a deprecated parameter in psycopg2 and not allowed in psycopg3. You will need to use dbname per the list here as found in page in the psycopg3 page for connect.

Related

Pynamodb Transaction not using local endpoint

Regardless of whatever I tried, pynamodb is not connecting to my local dynamodb.
Below is my model,
class OrderMarker(Model):
class Meta:
table_name = 'ddb_mrker_table'
connection = TableConnection(table_name='ddb_mrker_table', host='http://localhost:8000')
OrderUniqueueId = UnicodeAttribute(hash_key=True)
Below is the exception,
Traceback (most recent call last):
File "C:\backend\orders\snapshot\app.py", line 451, in <module>
main()
File "C:\backend\orders\snapshot\app.py", line 445, in main
OrderMarker.create_table(wait=True)
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\pynamodb\models.py", line 798, in create_table
if not cls.exists():
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\pynamodb\models.py", line 762, in exists
cls._get_connection().describe_table()
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\pynamodb\connection\table.py", line 294, in describe_table
return self.connection.describe_table(self.table_name)
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\pynamodb\connection\base.py", line 787, in describe_table
raise TableError("Unable to describe table: {}".format(e), e)
pynamodb.exceptions.TableError: Unable to describe table: Could not connect to the endpoint URL: "https://dynamodb.us-east-1.amazonaws.com/"
As you can see , pynamodb is trying to access https://dynamodb.us-east-1.amazonaws.com/ eventhough I have specified the local endpoint url. This is the case always, even when I try transactions too.
Local ddb server is run as shown below,
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -inMemory -delayTransientStatuses
A quick check using aws dynamodb list-tables --endpoint-url http://localhost:8000 shows local server is running fine!
Any idea?

Not able to connect to mysql using python

I am not able to connect to mysql using python. I have installed mysql-connector but not able to connect Please review my code and let me know where to make a change
I have gone through search engines but couldn't find a solution
import mysql.connector as mysql
mydb = mysql.connect(
host="localhost",
user="root",
password="",
)
Traceback (most recent call last): File
"C:\Users\varma\OneDrive\Documents\lib\site-packages\mysql\connector\network.py",
line 529, in open_connection
self.sock.connect(sockaddr) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively
refused it
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:/Users/varma/OneDrive/Documents/try.py", line 6, in
password="", File "C:\Users\varma\OneDrive\Documents\lib\site-packages\mysql\connector__init__.py",
line 173, in connect
return MySQLConnection(*args, **kwargs) File "C:\Users\varma\OneDrive\Documents\lib\site-packages\mysql\connector\connection.py",
line 104, in init
self.connect(**kwargs) File "C:\Users\varma\OneDrive\Documents\lib\site-packages\mysql\connector\abstracts.py",
line 780, in connect
self._open_connection() File "C:\Users\varma\OneDrive\Documents\lib\site-packages\mysql\connector\connection.py",
line 284, in _open_connection
self._socket.open_connection() File "C:\Users\varma\OneDrive\Documents\lib\site-packages\mysql\connector\network.py",
line 532, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err))) mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL
server on 'localhost:3306' (10061 No connection could be made because
the target machine actively refused it)

Django Dynamic models throws programming error

I use django-dynamic-model 0.1.0 for dynamic modelling in django.
I have already tried reverting the migrations, changing db. But nothing seems to work.
models.py:
from dynamic_models.models import AbstractModelSchema, AbstractFieldSchema
class ModelSchema(AbstractModelSchema):
pass
class FieldSchema(AbstractFieldSchema):
pass
views.py:
car_model_schema = ModelSchema.objects.create(name='Car')
I face error while creating ModelSchema object:
Internal Server Error: /api/v1.0/boilerplate_apps/graph/
Traceback (most recent call last):
File "/home/ubox72/Extras/graph/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "dynamic_models_modelfieldschema" does not exist
LINE 1: ...ynamic_models_modelfieldschema"."max_length" FROM "dynamic_m...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ubox72/Extras/graph/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
.......
django.db.utils.ProgrammingError: relation "dynamic_models_modelfieldschema" does not exist
LINE 1: ...ynamic_models_modelfieldschema"."max_length" FROM "dynamic_m...
Trying running this migration as well
python manage.py makemigrations dynamic_models
And then,
python manage.py migrate dynamic_models
This should solve this issue.

Having trouble catching an exception in python 3

Working with Python 3.7.3, still figuring out how exception handling works.
I'm writing an xmpp bot, using slixmpp. I'm trying to make it so that if it loses connection to the server, it will try to reconnect. There doesn't seem to be any way to do this built in to slixmpp, so I'm write something into my own code to do it.
I've imported slixmpp as xmpp, and using it's send_raw() method to test that we're still connected to the server.
while True:
time.sleep(5) # Send every 5 seconds just for testing purposes
xmpp.send_raw('aroo?')
When I sever the connection to the server, this is what it spits out:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "testcom.py", line 19, in run
eval(self.thing)()
File "testcom.py", line 28, in check_conn
xmpp.send_raw('aroo?')
File "C:\Program Files\Python37\lib\site-packages\slixmpp\xmlstream\xmlstream.py", line 926, in send_raw
raise NotConnectedError
slixmpp.xmlstream.xmlstream.NotConnectedError
I'm assuming that "NotConnectedError" is the exception that I need to catch, so I put the code inside a try block, like so:
try:
while True:
time.sleep(5) # Send every 5 seconds just for testing purposes
xmpp.send_raw('aroo?')
except NotConnectedError:
# Do a thing
pass
And this is what I get:
Traceback (most recent call last):
File "testcom.py", line 28, in check_conn
xmpp.send_raw('aroo?')
File "C:\Program Files\Python37\lib\site-packages\slixmpp\xmlstream\xmlstream.py", line 926, in send_raw
raise NotConnectedError()
slixmpp.xmlstream.xmlstream.NotConnectedError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
"testcom.py", line 19, in run
eval(self.thing)()
File "testcom.py", line 29, in check_conn
except NotConnectedError:
NameError: name 'NotConnectedError' is not defined
Can anyone tell me what I'm doing wrong here?
Thanks!
I can't see your imports but make sure you have from slixmpp.xmlstream.xmlstream import NotConnectedError otherwise it doesn't have a definition for NotConnectedError within the application. You could also change NotConnectedError to xmpp.xmlstream.xmlstream.NotConnectedError if you don't want to have it imported as well.

pexpect telnet on windows

I've used pexpect on linux successfully to telnet/ssh into Cisco switches for CLI scrapping. I'm trying to convert this code over to Windows and having some issues since it doesn't support the pxpect.spawn() command.
I read some online documentation and it suggested to use the pexpect.popen_spawn.PopenSpawn command. Can someone please point to me what I'm doing wrong here? I removed all my exception handling to simplify the code. Thanks.
import pexpect
from pexpect.popen_spawn import PopenSpawn
child = pexpect.popen_spawn.PopenSpawn('C:/Windows/System32/telnet 192.168.1.1')
child.expect('Username:')
child.sendline('cisco')
child.expect('Password:')
child.sendline('cisco')
child.expect('>')
child.close()
Error:
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 98, in expect_loop
incoming = spawn.read_nonblocking(spawn.maxread, timeout)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\popen_spawn.py", line 68, in read_nonblocking
raise EOF('End Of File (EOF).')
pexpect.exceptions.EOF: End Of File (EOF).
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python\windows scripts\telnet\telnet.py", line 37, in <module>
main()
File "C:\Python\windows scripts\telnet\telnet.py", line 20, in main
child.expect('Username:')
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\spawnbase.py", line 327, in expect
timeout, searchwindowsize, async_)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\spawnbase.py", line 355, in expect_list
return exp.expect_loop(timeout)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 104, in expect_loop
return self.eof(e)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python36\lib\site-packages\pexpect\expect.py", line 50, in eof
raise EOF(msg)
pexpect.exceptions.EOF: End Of File (EOF).
<pexpect.popen_spawn.PopenSpawn object at 0x000000000328C550>
searcher: searcher_re:
0: re.compile("b'Username:'")
The solution is to use plink.exe, which is a part of putty installation. You can download it from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
Enable TelnetClient on windows system and put plink.exe in the same folder from where you are running pexpect. Then you can telnet using pexpect as mentioned in the below example.
Also, use timeout flag with PopenSpawn to wait for the connection to establish. The above error is due to a timeout flag is not set.
p = pexpect.popen_spawn.PopenSpawn('plink.exe -telnet 192.168.0.1 -P 23', timeout=1)

Resources