我想连接到AWS服务器中的postgres。我已经安装了“ pg”到节点。 我尝试使用连接字符串,但是无法在其中添加私钥。所以我也尝试
# each subset of the collection.
def load_collections():
# Load the two collections.
book_collection, max_book_id = load_collection("books.csv")
movie_collection, max_movie_id = load_collection("movies.csv")
# Check for error.
if book_collection is None or movie_collection is None:
return None, None
# Return the composite dictionary.
return {"books": book_collection, "movies": movie_collection}, max(max_book_id, max_movie_id)
# Loads a single collection and returns the data as a list. Upon error, None is returned.
def load_collection(file_name):
max_id = -1
try:
# Create an empty collection.
collection = []
# Open the file and read the field names
collection_file = open(file_name, "r")
field_names = collection_file.readline().rstrip().split(",")
# Read the remaining lines, splitting on commas, and creating dictionaries (one for each item)
for item in collection_file:
field_values = item.rstrip().split(",")
collection_item = {}
for index in range(len(field_values)):
if (field_names[index] == "Available") or (field_names[index] == "Copies") or (field_names[index] == "ID"):
collection_item[field_names[index]] = int(field_values[index])
else:
collection_item[field_names[index]] = field_values[index]
# Add the full item to the collection.
collection.append(collection_item)
# Update the max ID value
max_id = max(max_id, collection_item["ID"])
# Close the file now that we are done reading all of the lines.
collection_file.close()
# Catch IO Errors, with the File Not Found error the primary possible problem to detect.
except FileNotFoundError:
print("File not found when attempting to read", file_name)
return None
except IOError:
print("Error in data file when reading", file_name)
return None
# Return the collection.
return collection, max_id
# Display the menu of commands and get user's selection. Returns a string with the user's requested command.
# No validation is performed.
def prompt_user_with_menu():
print("\n\n********** Welcome to the Collection Manager. **********")
print("COMMAND FUNCTION")
print(" ci Check in an item")
print(" co Check out an item")
print(" ab Add a new book")
print(" am Add a new movie")
print(" db Display books")
print(" dm Display movies")
print(" qb Query for books")
print(" qm Query for movies")
print(" x Exit")
return input("Please enter a command to proceed: ")
# Create the query function. Prompts user to enter query string for a book and
# displays ALL results--partial string matching and case insensitive. Note to self: book_collection is a list
# ORDER OF INFO in book_collection: title, author, publisher, pages, year, copies, available, ID
def query_book_collection(book_collection):
# check to see if the string is in the dictionary--partial string matching and case insensitive
query_string = input("Enter a query string to use for the search: ")
if query_string.lower() in book_collection:
print(book_collection)
else:
print("Sorry, that search returned no results.")
def query_movie_collection():
pass
def check_out():
pass
def check_in():
pass
def get_item_ID():
pass
def display_collection():
pass
def add_book():
pass
def add_movie():
pass
# This is the main program function. It runs the main loop which prompts the user and performs the requested actions.
def main():
# Load the collections, and check for an error.
library_collections, max_existing_id = load_collections()
if library_collections is None:
print("The collections could not be loaded. Exiting.")
return
print("The collections have loaded successfully.")
# Display the error and get the operation code entered by the user. We perform this continuously until the
# user enters "x" to exit the program. Calls the appropriate function that corresponds to the requested operation.
operation = prompt_user_with_menu()
while operation != "x":
if operation == "ci":
check_in(library_collections)
elif operation == "co":
check_out(library_collections)
elif operation == "ab":
max_existing_id = add_book(library_collections["books"], max_existing_id)
elif operation == "am":
max_existing_id = add_movie(library_collections["movies"], max_existing_id)
elif operation == "db":
display_collection(library_collections["books"])
elif operation == "dm":
display_collection(library_collections["movies"])
elif operation == "qb":
query_book_collection(library_collections["books"])
elif operation == "qm":
query_movie_collection(library_collections["movies"])
else:
print("Unknown command. Please try again.")
operation = prompt_user_with_menu()
# Start the program.
main()
在两种情况下,我都被拒绝连接。