I am creating a Python script to interact with schema permissions (and relative tables) on Redshift. As suggested in some other StackOverflow posts I am using psycopg2 library.
When I try to execute some simple SELECT FROM queries I have no problems: I can execute and see results with no issues.
Problem comes when for example I try to create a new schema or granting / revoking permissions. This kind of queries don't look like to produce any effect.
Here I show a very simple example in which I attempt to create a new schema:
conn_string = "dbname='{}' port='{}' host='{}' user='{}' password='{}'".format(DB_NAME, DB_PORT, DB_HOST, DB_USER, DB_PWD)
con = psycopg2.connect(conn_string)
sql = "CREATE SCHEMA new_schema"
cur = con.cursor()
cur.execute(sql)
But when I look into the Redshift DB I don't see any new schema called new_schema. Same behaviour happens when I try to run some permission grant / revoke query.
Anyone knows what is going on?
You have to commit the transaction.
con = psycopg2.connect(conn_string)
sql = "CREATE SCHEMA new_schema"
cur = con.cursor()
cur.execute(sql)
con.commit()
Related
I'm trying to use the python module: redshift_connector to create a materialized view in a postgresql redshift database. My query is akin to:
query = """CREATE MATERIALIZED VIEW some_results AS SELECT * FROM other_results;"""
I pass this into the function below and nothing happens, however if I log into the database via terminal and run the same commands it works. Is this a permissions thing, or is there something wrong with my connector code? I can read from tables in the database with the connector without an issue.
def redshift_query(query)
conn = redshift_connector.connect(
host=XXXX,
port=5439,
database=XXXX,
user=XXXX,
password=XXXX)
print('Connected to Redshift...')
with conn.cursor() as cursor:
print('Querying Database...')
t1 = time.time()
cursor.execute(query)
t2 = time.time()
print(f'command sent, this took {t2-t1}s')
pass```
i've been trying to get some data from my db by using below code, but the code is not working. is there any mistake that i made in the code, if so how can i fix it.
NOTE: i took the below code from just a script not a django or flesk web app.
def db():
conn = psycopg2.connect(
"dbname=mydb user=postgres password=****** host=*.*.*.*")
cur = conn.cursor()
cur.execute("""SELECT * FROM MddPublisher""")
query_results = cur.fetchall()
print(query_results)
db()
ERROR: psycopg2.errors.UndefinedTable: relation "mddpublisher" does not exist LINE 1: SELECT * FROM MddPublisher
additionally,i want to show below code to prove that connection is ok. the problem is that i can't receive data from my db whenever i try to execute select command through python.
def print_tables():
conn = psycopg2.connect(
"dbname=mydb user=postgres password=***** host=*.*.*.*.*")
cur = conn.cursor()
cur.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cur.fetchall():
print(table)
print_tables()
OUTPUT:
('MddPublisher',)
This is probably an issue with case sensitivity. Postgresql names are usually normalized to lower case. However, when used inside double quotes, they keep their case. So, to access a table named MddPublisher you must write it like "MddPublisher".
All the gory details are in Section 4.1.1, Identifiers and Key Words in the Postgresql 14 docs.
I have a postgres db running on docker. I am able to access this db via my sql client Dbeaver and when I run select statements I see the expected results.
I would like to be able to query this db via a Python script and after some searching found psycopg2 package.
When I run the code below it 'looks' like it's successful, the conn and cursor objects appear as a variables.
import pandas as pd
import psycopg2
# connect to db
conn = psycopg2.connect(
host="localhost",
database="postgres",
user="postgres",
password="example")
# create a cursor
cur = conn.cursor()
However, when trying to query the db using cur.connect(), , variable ex_data is None. This exact same query via my sql client returns a table of data.
ex_data = cur.execute('select * from myschema.blah limit 10;')
How can I query my db via Python using psycopg2? Desired result wold be a data frame with the result set from the query string above.
I'm using the Python package MySQLdb to fetch data from a MySQL database. However, I notice that I can't fetch the entirety of the data.
import MySQLdb
db = MySQLdb.connect(host=host, user=user, passwd=password)
cur = db.cursor()
query = "SELECT count(*) FROM table"
cur.execute(query)
This returns a number less than what I get if I execute the exact same query in MySQL Workbench. I've noticed that the data it doesn't return is the data that was inserted into the database most recently. Where am I going wrong?
You are not committing the inserted rows on the other connection.
I am testing Python and Mysql in that i am able to create and delete table's but i am unable to insert data in them.I searched stackoverflow and mostly they suggest to use
commit()
So i used it and even after i used the data is not inserted into the database.Please help me.
This is the code i use it creates the table but not inserting data
import MySQLdb
db = MySQLdb.connect("localhost","user","password")
cxn = MySQLdb.connect(db='test')
cursor = cxn.cursor()
cursor.execute("CREATE TABLE users(name VARCHAR(40),id VARCHAR(40))")
cursor.execute("INSERT INTO users(name,id) VALUES('John','1')")
db.commit()
print "Opertion completed successfully"
Are db and cxn connections to the same database?
You should establish your connection using following:
db = MySQLdb.connect(host="localhost",
db="test",
user="user",
passwd="password")
The cursor should then be derived from this connection via:
cursor = db.cursor()
I would hazard that your issue is coming from the ambiguity between db and cxn.