[英]Error while connecting to Database/postgresql/python
What I am trying to do is save an image to my PostgreSQL database, I found a code that helps with this.我想要做的是将图像保存到我的 PostgreSQL 数据库中,我找到了一个有助于此的代码。 But I am having difficulty pushing the image into the database as this error keep popping out:但是我很难将图像推送到数据库中,因为这个错误不断弹出:
Error while connecting to PostgreSQL [Errno 13] Permission denied: 'C:/Users/hc559/Downloads/facial-master/facial-master/dataset/adrian' Po连接到 PostgreSQL 时出错 [Errno 13] 权限被拒绝:'C:/Users/hc559/Downloads/facial-master/facial-master/dataset/adrian' Po
stgreSQL connection is closed stgreSQL 连接已关闭
I tried to resolve by setting the file permission to everyone but to no avail.我试图通过为所有人设置文件权限来解决,但无济于事。
import sys
import psycopg2
import argparse
try:
connection = psycopg2.connect(user = "postgres",
password = "123",
host = "127.0.0.1",
port = "5432",
database = "postgres")
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print ( connection.get_dsn_parameters(),"\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")
def main(argv):
parser = argparse.ArgumentParser()
parser_action = parser.add_mutually_exclusive_group(required=True)
parser_action.add_argument("--store", action='store_const', const=True, help="Load an image from the named file and save it in the DB")
parser_action.add_argument("--fetch", type=int, help="Fetch an image from the DB and store it in the named file, overwriting it if it exists. Takes the database file identifier as an argument.", metavar='42')
parser.add_argument("filename", help="Name of file to write to / fetch from")
args = parser.parse_args(argv[1:])
args = {
'store': 'C:/Users/L30809.NYPSIT.000/Downloads/new/opencv-face-recognition/facial/dataset/Avan',
'fetch': 'C:/Users/L30809.NYPSIT.000/Downloads/new/opencv-face-recognition/facial/dataset/Avan',
'filename': 'C:/Users/hc559/Downloads/facial-master/facial-master/dataset/adrian'
}
# and run the command
if args['store']:
# Reads the whole file into memory. If you want to avoid that,
# use large object storage instead of bytea; see the psycopg2
# and postgresql documentation.
f = open(args['filename'],'rb')
# The following code works as-is in Python 3.
#
# In Python 2, you can't just pass a 'str' directly, as psycopg2
# will think it's an encoded text string, not raw bytes. You must
# either use psycopg2.Binary to wrap it, or load the data into a
# "bytearray" object.
#
# so either:
#
# filedata = psycopg2.Binary( f.read() )
#
# or
#
# filedata = buffer( f.read() )
#
filedata = f.read()
cursor.execute("INSERT INTO files(id, orig_filename, file_data) VALUES (DEFAULT,%s,%s) RETURNING id", (args['filename'], filedata))
returned_id = cursor.fetchone()[0]
f.close()
connection.commit()
print("Stored {0} into DB record {1}".format(args['filename'], returned_id))
elif args['fetch'] is not None:
# Fetches the file from the DB into memory then writes it out.
# Same as for store, to avoid that use a large object.
f = open(args['filename'],'wb')
cursor.execute("SELECT file_data, orig_filename FROM files WHERE id = %s", (int(args['fetch']),))
(file_data, orig_filename) = cursor.fetchone()
# In Python 3 this code works as-is.
# In Python 2, you must get the str from the returned buffer object.
f.write(file_data)
f.close()
print("Fetched {0} into file {1}; original filename was {2}".format(args['fetch'], args['filename'], orig_filename))
connection.close()
if __name__ == '__main__':
main(sys.argv)
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
You need to adjust the database and table permissions permissions for the conneting user.您需要调整连接用户的数据库和表权限。 You do this in the file pg_hba.conf or by using a statement like 'grant on to ' .您可以在文件 pg_hba.conf 中执行此操作,或者使用诸如 'grant on to' 之类的语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.