简体   繁体   English

如何通过 AWS Jupyter Notebook 查询(Postgres)RDS 数据库?

[英]How to query a (Postgres) RDS DB through an AWS Jupyter Notebook?

I'm trying to query an RDS (Postgres) database through Python, more specifically a Jupyter Notebook.我正在尝试通过 Python 查询 RDS(Postgres)数据库,更具体地说是 Jupyter Notebook。 Overall, what I've been trying for now is:总的来说,我现在一直在尝试的是:

import boto3

client = boto3.client('rds-data')

response = client.execute_sql(
    awsSecretStoreArn='string',
    database='string',
    dbClusterOrInstanceArn='string',
    schema='string',
    sqlStatements='string'
)

The error I've been receiving is:我一直收到的错误是:

BadRequestException: An error occurred (BadRequestException) when calling the ExecuteSql operation: ERROR: invalid cluster id: arn:aws:rds:us-east-1:839600708595:db:zprime

In the end, it was much simpler than I thought, nothing fancy or specific.最后,它比我想象的要简单得多,没有什么特别的或具体的。 It was basically a solution I had used before when accessing one of my local DBs.它基本上是我之前在访问我的本地数据库时使用过的一种解决方案。 Simply import a specific library for your database type (Postgres, MySQL, etc) and then connect to it in order to execute queries through python.只需为您的数据库类型(Postgres、MySQL 等) import一个特定的库,然后连接到它以通过 python 执行查询。

I don't know if it will be the best solution since making queries through python will probably be much slower than doing them directly, but it's what works for now.我不知道这是否是最好的解决方案,因为通过 python 进行查询可能比直接进行查询要慢得多,但它现在是有效的。

import psycopg2

conn = psycopg2.connect(database = 'database_name',
                        user =     'user',
                        password = 'password',
                        host =     'host',
                        port =     'port')

cur = conn.cursor()

cur.execute('''
            SELECT *
            FROM table;
            ''')

cur.fetchall()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM