I have created a python file app.py and included the code to connect to a db I created in postgresql as follows: -
import psycopg2
conn = psycopg2.connect(
user='postgres',
password='1234',
host='localhost',
port='5432',
database='bubbleformation'
)
cursor = conn.sursor()
cursor.execute('SELECT * FROM bubbleformation')
for row in cursor: print(row)
conn.close()
This was as instructed in this medium article
However, when I try to execute this python file in the terminal, I get the below error: -
Traceback (most recent call last): File "app.py", line 8, in
port='5432' File "/usr/lib/python2.7/dist-packages/psycopg2/init.py", line 130, in
connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: FATAL: database "bubbleformation" does not exist
I have created a table named "bubbleformation" and it can be viewed in the psql mode through the terminal.
Could anyone please help me understand what should be done? I tried changing the password, and user privileges, but none of them worked for my error.
You should create both database and table with the same name "bubbleformation". You've probably created that table in postgres database.
Enter psql as postgres user and call CREATE DATABASE bubbleformation;, then connect to it with \connect bubbleformation and then create your table (something like CREATE TABLE bubbleformation (id int, name text);).
The error is about there not being a database named "bubbleformation", so when you connect to the database in the terminal, whichever database that is is the one you need to specify in the database parameter. When you connect to the database in the terminal, type:
SELECT current_database();
If it is indeed a database named "bubbleformation" then is must be a different cluster you are connecting to, and therefore a different port.
Disclosure: I am an EnterpriseDB (EDB) employee.
Its due to environment error. I was loading the credentials from the .env file. But i mistakenly gave a wrong path.
project_folder = os.path.expanduser('~/scraping') instead of
project_folder = os.path.expanduser('~/find_my_nearest_store')
load_dotenv(os.path.join(project_folder, '.env'))
Hence the Error.
Related
Basically trying to connect a flask app to a sqlite database file on a networked windows file server using flask-sqlalchemy (fully aware of the considerations when using a sqlite database over a network.) The flask app was not connecting to the database and I was using sqlalchemy to troubleshoot.
I can connect with the following line:
sq2_db = sqlite3.connect(r"//ppcou.ucsc.edu\Data\Archive_Data\archives_app.db")
...but the following elicit an error:
db = sqlalchemy.create_engine(r"sqlite:////ppcou.ucsc.edu\Data\Archive_Data\archives_app.db")
db.connect()
db = sqlalchemy.create_engine(r"sqlite:///ppcou.ucsc.edu\Data\Archive_Data\archives_app.db")
db.connect()
Error:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
(Background on this error at: https://sqlalche.me/e/14/e3q8)
FYI This solves to True:
os.path.exists(r'\\ppcou.ucsc.edu\Data\Archive_Data\archives_app.db')
Any thoughts?
Gord Thompson's comment was correct. Adding an addition slash was the remedy.
db = sqlalchemy.create_engine(r"sqlite://///ppcou.ucsc.edu\Data\Archive_Data\archives_app.db")
db.connect()
I want to connect to a postgres database in a specific squema using psycopg2. I read this thread but I get a syntax error.
schema = conf['schema']
conn = psycopg2.connect(
dbname=conf['database'],
user=conf['user'],
host=conf['host'],
password=conf['passw'],
port=conf['port'],
options=f'-c search_path={schema}',
)
I checked the config.json where I get the data.
Thanks
I have a database that I am running on my local machine which I can access through Microsoft SQL Server Manager Studio. I connect to this server "JIMS-LAPTOP\SQLEXPRESS" and then I can run queries through the manager. However I need to be able to connect to this database and work with it through python.
When I try to connect using sqlite3 like
conn = sqlite3.connect("JIMS-LAPTOP\SQLEXPRESS")
I get an unable to open database file error
I tried accessing the temporary file directly like this
conn = sqlite3.connect("C:\Users\Jim Notaro\AppData\Local\Temp\~vs13A7.sql")
c = conn.cursor()
c.execute("SELECT name FROM sqlite_master WHERE type = \"table\"")
print c.fetchall()
Which allows me to access a database but it is completely empty (No tables are displayed)
I also tried connecting like this
conn = sqlite3.connect("SQL SERVER (SQLEXPRESS)")
Which is what the name is in the sql server configuration manager but that also returns a blank database.
I'm not sure how I am suppose to be connecting to the database using python
You can't use sqlite3 to connect to SQL server, only to Sqlite databases.
You need to use a driver that can talk to MS SQL, like pyodbc.
I got the following code from this site to connect to a mysql database through python (which is much appreciated by the way). I have used it once already to connect to a database on the server, but in a different file. However I know want to connect to another database on the same server but i keep getting error message to do with the database name.
I am using exactly the same code as I was before to connect, however i have only changed the database name to the other one i want to connect to. I can look at the database through puTTY, and see that it is called the correct name and that all the data is there, but just cant seem to connect properly through the Python.
Thanks in advance for any help!
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="127.0.0.1", # your host, usually localhost
user="root", # your username
passwd="........", # your password
db="opt_out") # name of the data base
# you must create a Cursor object. It will let
# you execute all the query you need
cur = db.cursor()
# Use all the SQL you like
cur.execute("SELECT * FROM opted_in")
# print all the first cell of all the rows
for row in cur.fetchall() :
print row[0]
And this is the error code i get in return when i try and execute the python file through puTTY:
Traceback (most recent call last):
File "/var/www/html/vra/ConnectAdiDB.py", line 7, in <module>
db="opt_out") # name of the data base
File "/usr/lib64/python2.6/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'#'localhost' (using password: YES)")
The Password must be wrong (I Think so)
So Try to reset the password so:
first off turn of the mysql Service like so
Ubuntu/Debian:
sudo service mysql stop
After That Do So:
sudo service mysql start
When Done Start mysql like so:
sudo mysql
An execute:
UPDATE mysql.user SET Password=PASSWORD('NEW-PASSWORD') WHERE User='root';
FLUSH PRIVILEGES;
I have created a user for my Postgres database but I cannot access the data with the user that I set up.
In a python shell I have run the following code:
>>> import psycopg2
>>> conn=psycopg2.connect(
database="db",
user="postgres",
host="/tmp/",
password="opensesame"
)
>>> cur = conn.cursor()
>>> state = 'Arkansas'
>>> cur.execute("SELECT city FROM table WHERE state = %s", (state,))
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
cur.execute("SELECT city FROM table WHERE state = %s", (state,))
ProgrammingError: permission denied for relation table
As the superuser, I have tried:
db=# ALTER USER postgres SET TO default
but it did not help
I need to get this user access to this table, any help would be appreciated. Thank you for viewing.
try this,
log into postgres
sudo -u postgres psql
then enter \l it will list all the tables, you will see a list of your created databases,
then
GRANT ALL ON db TO postgres;
if all goes well you should now do \l again and see that you are now privileged.
In my code, I was receiving the error message:
ProgrammingError: permission denied for relation author
Although:
GRANT ALL ON db TO postgres;
may resolve this error for some, I later located:
Permission denied for relation
and found that you may want to name the relation, sequence or table instead of the DB. According to the link above, granting privilege on the DB might have more to do with accessing the connection, rather than sort of recursively going through the tables, relations and sequences and granting privilege on all of them.
At the very least, if you run into this error, read both this article and that one and see if either one resolves the problem.