Adicione o SQLAlchemy 2 e o psycopg3 ao arquivo
requirements.txt.
psycopg~=3.1.8sqlalchemy~=2.0.1
Especifique postgresql+psycopg, localhost e 5432 como o dialeto do banco de dados,
driver, host do servidor e porta na string de conexão do SQLAlchemy 2.
O psycopg3 exige um nome de usuário e uma senha na string de
conexão. O PGAdapter ignora esses valores.
Você também pode especificar um número de porta diferente se o PGAdapter estiver
configurado para detectar em uma porta diferente da porta padrão do PostgreSQL (5432).
Usar o SQLAlchemy 2 com bancos de dados de dialeto PostgreSQL
Para mais informações sobre os recursos e as recomendações do
SQLAlchemy 2 com bancos de dados de dialeto PostgreSQL, consulte a
documentação de referência
no GitHub.
A seguir
Confira o sample-application usando
o SQLAlchemy 2 com o PGAdapter e o Spanner.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-17 UTC."],[],[],null,["# Integrate Spanner with SQLAlchemy 2 ORM (PostgreSQL dialect)\n\n[SQLAlchemy 2](https://docs.sqlalchemy.org/en/20/) is a Python SQL toolkit and Object\nRelational Mapper (ORM).\n\nYou can use the SQLAlchemy 2 ORM in combination with\nthe standard [PostgreSQL psycopg3 driver](https://www.psycopg.org/psycopg3/) and PGAdapter.\n\nSet up SQLAlchemy 2 with Spanner PostgreSQL-dialect databases\n-------------------------------------------------------------\n\n1. Ensure that PGAdapter is running on the same machine as the\n application that is connecting using SQLAlchemy 2 with Spanner.\n\n For more information, see [Start PGAdapter](/spanner/docs/pgadapter-start).\n2. Add SQLAlchemy 2 and psycopg3 to your\n `requirements.txt` file.\n\n psycopg~=3.1.8\n sqlalchemy~=2.0.1\n\n3. Specify `postgresql+psycopg`, `localhost` and `5432` as the database dialect,\n driver, server host and port in the SQLAlchemy 2 connection string.\n psycopg3 requires a username and password in the connection\n string. PGAdapter ignores these.\n\n Optionally, specify a different port number if PGAdapter is\n configured to listen on a port other than the default PostgreSQL\n port (5432). \n\n conn_string = \"postgresql+psycopg://user:password@localhost:5432/my-database\"\n engine = create_engine(conn_string)\n\nSee the [SQLAlchemy 2 with PostgreSQL documentation](https://docs.sqlalchemy.org/en/20/dialects/postgresql.html#dialect-postgresql)\nfor more connection options for PostgreSQL.\n\nUse SQLAlchemy 2 with PostgreSQL-dialect databases\n--------------------------------------------------\n\nFor more information about the features and recommendations for\nSQLAlchemy 2 with PostgreSQL-dialect databases, please consult the\n[reference documentation](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/python/sqlalchemy2-sample)\non GitHub.\n\nWhat's next\n-----------\n\n- Check out the [sample-application](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/python/sqlalchemy2-sample/sample.py) using SQLAlchemy 2 with PGAdapter and Spanner.\n- Learn more about [SQLAlchemy](https://docs.sqlalchemy.org/en/20/).\n- Learn more about [PGAdapter](/spanner/docs/pgadapter).\n- [File a GitHub issue](https://github.com/GoogleCloudPlatform/pgadapter/issues) to report a bug or ask a question about Spanner dialect for SQLAlchemy with PGAdapter."]]