Struggling to remotly query a postgres database.
I've populated the /etc/postgresql/12/main/pg_hba.conf / postgresql.conf with the correct information.
I know I can query remotely as its working from within PyCharm.
But EVERY time I try this command I'm getting an error
conn = psycopg2.connect(host="******", database="******", user="******", password="********")
cur = conn.cursor()
cur.execute(f"""select id, login_count, orders from "user" where account = '{account}' """)
ERROR:
permission denied for table user
Postgresql log:
2021-09-08 23:03:52.353 UTC [11354] colemanbros#colemans ERROR: permission denied for table user
2021-09-08 23:03:52.353 UTC [11354] colemanbros#colemans STATEMENT: select * from "user"
The login credentials for both pycharm and psycopg2 are the same. And both are being run from my local machine.
Psycopg2 on jupyter
Pycharm is connecting to the database with jdbc
Can anyone offer some insight into why this might be?
Related
I am following along with lecturer's code and videos. He has this set up, and I have followed exactly. His works, mine doesn't and I cant figure out why. It is set up as user "root" and password is blank. I have tried pip install mysql-connector-python. I want to keep the same user and password as his so as to follow along better. I am using python and mysql via Wampserver64. When I try to run the python file through cmd I get the error "mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)". I am new to this so trying to figure it out as I go along. Does the (using password: YES) mean that the passwords match? And how to I get script to connect to mysql?
db = mysql.connector.connect(
host = "localhost",
user= "root",
password = " "
#database ='datarepresentation'
)
#print ("connection made")
cursor = db.cursor()
cursor.execute("CREATE DATABASE datarepresentation")
The same problem occurred when my friend tried to run a python script in the Ubuntu Windows Linux Subsystem that uses a MySQL database set up.
We fixed the problem by running the following three commands in the MySQL 8.0 Command Line Client and then restarting the machine to reboot everything. We are using Flask in our project and not Wamp so hopefully it will work the same. These commands were found here.
SELECT user, authentication_string, plugin, host FROM mysql.user;
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'Current-Root-Password';
FLUSH PRIVILEGES;
According to MySQL documentation(MySQL Documentation), it states that (using password: YES) just means that you are in fact using a password. If you would have tried to login without using a password, it would say 'NO'.
As far as how to connect your script to your database, you pretty much have it. You can write a query to retrieve something from the database to check. Here is an example using the database you mentioned to retrieve some kind of data and make sure it's in the table.
cnx = mysql.connector.connect(
host="localhost",
user='root',
password=" ",
database='datarepresentation')
cursor = cnx.cursor()
query = ("SELECT * FROM table-name WHERE key1 = %s")
dataName = 'randomValue'
cursor.execute(query, (dataName))
result = cursor.fetchone()
if result[0] == 1:
return True
else:
return False
Don't assign any value to password argument and pass as i.e password=''
try to create new user
mysql> CREATE USER 'monty'#'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
-> WITH GRANT OPTION;
mysql> CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
-> WITH GRANT OPTION;
I also encountered it and solve such as below.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
#remove this line or password=""
)
It's remove the line of the password. if you want to insert the line of the password,it's unwanted the spase(for instance,password="", this isn't password=" ").
this program is (using password: YES).
Why can't i solve?
I thought that this error is indicating already "The passwords match."
so,description of the password is unwanted in mydb=mysql.connector.connect( ) .
I have specified how to solve on my site [troubleshooting] ProgrammingError: **** (*****): Access denied for user 'root'#'localhost' (using password: YES) mysql-connector of python.
I have a sqlalchemy connection setup to snowflake which works, as I can run some queries and get results out. The attempts to query are also logged in my user_query history.
My connection:
engine = create_engine(URL(
user, password, account, database, warehouse, role
))
connection = engine.connect()
However, most of the time my queries fail returning Operational Error (i.e. its a snowflake error) https://docs.sqlalchemy.org/en/13/errors.html#error-e3q8. But these same queries will run fine in the snowflake web UI.
For example if I run
test_query = 'SELECT * FROM TABLE DB1.SCHEMA1.TABLE1'
test = pd.read_sql(test_query, connection)
When I look at my query_history it shows the sqlalchemy query failing, then a second later the base query itself being run successfully. However I'm not sure where this output goes in the snowflake setup, and why its not transferring through my sqlalchemy connection. What I'm seeing...
Query = 'DESC TABLE /* sqlalchemy:_has_object */ "SELECT * FROM DB1"."SCHEMA1"."TABLE1"
Error code = 2003 Error message = SQL compilation error: Database
'"SELECT * FROM DB1" does not exist.
Then 1 second later, the query itself will run successfully, but not clear where this goes as it doesn't get sent over the connection.
Query = SELECT * FROM TABLE DB1.SCHEMA1.TABLE1
Any help much appreciated!
Thanks
You can try adding schema also here
engine = create_engine(URL(
account = '',
user = '',
password = '',
database = '',
schema = '',
warehouse = '',
role='',
))
connection = engine.connect()
It is very unlikely that the query is running in WebUI and fails with syntax error when connected via CLI or other modes.
Suggest you print the query which is via CLI or via a connector, run the same to WebUI and also note that from which role you're running the query.
Please share what is your finding.
The mentioned query (SELECT * FROM TABLE DB1.SCHEMA1.TABLE1) is not a snowflake supported SQL syntax.
Link here will help you more with details.
Hope this helps!
So I have a successful install of Postgres on Ubuntu and am trying to do some basic connection and creating a table in a db using another username other than the default (postgres) and coming up short. From what I can gather I think it may have something to do with permissions? What I want is to be able to use some superuser other than postgres to create tables and do stuff.
"psql example3" and "\l" shows the example3 db was created successfully. I now have a list of databases include the default postgres, template0, template1 and example3 all with owner as postgres. What I run into problems then is running demoscript.py gives a fatal "peer authentication failed for user 'thisuser'"
#Create the db and user with superuser permissions
sudo -u postgres -i
createdb example3
createuser --interactive --pwprompt
#role:thisuser
#pwd:thispass
#superuser?:Y
#demoscript.py
import psycopg2
connection = psycopg2.connect('dbname=example3 user=thisuser password=thispass')
cursor = connection.cursor()
cursor.execute('DROP TABLE IF EXISTS todos;')
cursor.execute('''
CREATE TABLE todos(
id serial PRIMARY KEY,
description VARCHAR NOT NULL
);
''')
connection.commit()
cursor.close()
connection.close()
Expected result is that the todos table should show as created after looking for it in the example3 db. But I just get the fatal error.
When you don't specify a host in your connection, it tries to connect via Unix sockets. By default, PostgreSQL is set up to use peer authentication on those, which means it compares the PostgreSQL username to the currently logged in OS user. If you change your connection to:
connection = psycopg2.connect('dbname=example3 user=thisuser password=thispass host=localhost')
that should cause it to use the authentication settings for TCP/IP connections, which default to password authentication on most systems I've used.
Not able to connect to Azure DB. I get the following error while connecting via Python.
I'm able to connect to my usual SQL environment
import pandas as pd
import pymssql
connPDW = pymssql.connect(host=r'dwprd01.database.windows.net', user=r'internal\admaaron',password='',database='')
connPDW.autocommit(True)
cursor = connPDW.cursor()
conn.autocommit(True)
cursor = conn.cursor()
sql = """
select Top (10) * from TableName
"""
cursor.execute(sql);
Run without errors.
Just according to your code, there is an obvious issue of connecting Azure SQL Database by pymssql package in Python which use the incorrect user format and lack of the values of password and database parameters.
Please follow the offical document Step 3: Proof of concept connecting to SQL using pymssql carefully to change your code correctly.
If you have an instance of Azure SQL Database with the connection string of ODBC, such as Driver={ODBC Driver 13 for SQL Server};Server=tcp:<your hostname>.database.windows.net,1433;Database=<your database name>;Uid=<username>#<host>;Pwd=<your_password>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30; show in the Connection strings tab of your SQL Database on Azure portal.
Then, your code should be like below
hostname = '<your hostname>'
server = f"{hostname}.database.windows.net"
username = '<your username>'
user = f"{username}#{hostname}"
password = '<your password>'
database = '<your database name>'
import pymssql
conn = pymssql.connect(server=server, user=user, password=password, database=database)
Meanwhile, just additional note for the version of Azure SQL Database and MS SQL Server are 2008+ like the latest Azure SQL Database, you should use the ODBC Driver connection string which be started with DRIVER={ODBC Driver 17 for SQL Server};, not 13 show in the connection string of Azure portal if using ODBC driver for Python with pyodbc, please refer to the offical document Step 3: Proof of concept connecting to SQL using pyodbc.
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.