You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/client-libraries/python.mdx
+38-21Lines changed: 38 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -11,8 +11,8 @@ import CodeSnippet from '/components/code-snippet/CodeSnippet'
11
11
12
12
Learn how to create a Python application that connects to the Memgraph database and executes simple queries.
13
13
14
-
Both [Neo4j Python client](https://neo4j.com/docs/python-manual/current/) and [GQLAlchemy](https://github.com/memgraph/gqlalchemy) can be used to connect to Memgraph with Python.
15
-
This guide will show how to use Neo4j Python client and for more information on GQLAlchemy, check out its [documentation](https://memgraph.github.io/gqlalchemy/).
14
+
Both [Neo4j Python client](https://neo4j.com/docs/python-manual/current/) and [GQLAlchemy](https://github.com/memgraph/gqlalchemy) can be used to connect to Memgraph with Python.
15
+
This guide will show how to use Neo4j Python client and for more information on GQLAlchemy, check out its [documentation](https://memgraph.github.io/gqlalchemy/).
16
16
17
17
Memgraph and Neo4j both support Bolt protocol and Cypher queries, which means that same client can be used to connect to both databases.
18
18
This is very convenient if switching between the two databases is needed. This guide is based on the client version v5 and above. Some examples may
@@ -29,7 +29,7 @@ Necessary prerequisites that should be installed in your local environment are:
29
29
30
30
<Steps>
31
31
32
-
### Run Memgraph
32
+
### Run Memgraph
33
33
34
34
If you're new to Memgraph or you're in a developing stage, we
35
35
recommend using the Memgraph Platform. Besides the database, it also
@@ -60,7 +60,7 @@ communicate with the client using the exposed 7687 port. Memgraph Lab is a web
60
60
application you can use to visualize the data. It's accessible at
61
61
[http://localhost:3000](http://localhost:3000) if Memgraph Platform is running
62
62
correctly. The 7444 port enables Memgraph Lab to access and preview the logs,
63
-
which is why both of these ports need to be exposed.
63
+
which is why both of these ports need to be exposed.
64
64
65
65
For more information visit the getting started guide on [how to run Memgraph
66
66
with Docker](/getting-started/install-memgraph/docker).
@@ -178,6 +178,7 @@ Once the database is running and the client is installed or available in Python,
178
178
-[Connect with authentication](#connect-with-authentication)
179
179
-[Connect with self-signed certificate](#encrypted-database-connection-with-self-signed-certificate)
180
180
-[Connect with Single sign-on (SSO)](#connect-with-single-sign-on-sso)
181
+
-[Impersonate a user](#impersonate-a-user)
181
182
182
183
#### Connect without authentication (default)
183
184
@@ -295,6 +296,22 @@ with neo4j.GraphDatabase.driver(
295
296
) as driver:
296
297
```
297
298
299
+
#### Impersonate a user
300
+
301
+
<Callouttype="info">
302
+
[User impersonation](/database-management/authentication-and-authorization/impersonate-user) is an Enterprise feature.
303
+
</Callout>
304
+
305
+
Once logged in, a user with the correct permissions can impersonate a different
306
+
users during a session. This means that any query executing during that session
307
+
will be executed as if the impersonated user executed it. The target user can be
308
+
defined during session creation as in the following snippet:
309
+
310
+
```python
311
+
with driver.session(impersonated_user="user1") as session:
312
+
# queries here will be executed as if user1 executed them
313
+
```
314
+
298
315
### Query the database
299
316
300
317
After connecting your client to Memgraph, you can start running queries. The simplest way to run queries is by using the `execute_query()` method which has an automatic transaction management.
@@ -424,8 +441,8 @@ Path will contain [Nodes](#process-the-node-result) and [Relationships[#process-
424
441
425
442
Transaction is a unit of work that is executed on the database, it could be some basic read, write or complex set of steps in form of series of queries. There can be multiple ways to mange transaction, but usually, they are managed automatically by the client or manually by the explicit code steps. Transaction management defines how to handle the transaction, when to commit, rollback, or terminate it.
426
443
427
-
On the driver side, if a transaction fails because of a transient error, the transaction is retried automatically.
428
-
The transient error will occur during write conflicts or network failures. The driver will retry the transaction function with an exponentially increasing delay.
444
+
On the driver side, if a transaction fails because of a transient error, the transaction is retried automatically.
445
+
The transient error will occur during write conflicts or network failures. The driver will retry the transaction function with an exponentially increasing delay.
429
446
430
447
#### Automatic transaction management
431
448
@@ -671,9 +688,9 @@ The `Session.run()` method is most commonly used for `LOAD CSV` clause to preven
671
688
672
689
#### Concurrent transactions
673
690
674
-
It is possible to run concurrent transactions with Python's client by leveraging threads or processes.
675
-
Using threads could cause your code to be partially locked because of [Global interpreter lock (GIL)](https://wiki.python.org/moin/GlobalInterpreterLock),
676
-
resulting in slow execution. Hence, it is always better to run your workloads in separate processes,
691
+
It is possible to run concurrent transactions with Python's client by leveraging threads or processes.
692
+
Using threads could cause your code to be partially locked because of [Global interpreter lock (GIL)](https://wiki.python.org/moin/GlobalInterpreterLock),
693
+
resulting in slow execution. Hence, it is always better to run your workloads in separate processes,
677
694
where each process will have its own interpreter and memory space, avoiding GIL issues. To leverage multiple concurrent processes, you can use Python's `multiprocessing` module.
678
695
679
696
Here is an example of how to run concurrent transactions with `multiprocessing` module:
@@ -685,7 +702,7 @@ from neo4j import GraphDatabase
The `GRANT IMPERSONATE_USER` query allows a user or role to impersonate specific users or all users. The syntax and behavior are as follows:
58
+
59
+
```cypher
60
+
GRANT IMPERSONATE_USER [*] [list of users] TO user/role;
61
+
```
62
+
63
+
Here is the explanation of arguments:
64
+
-`*`: Grants permission to impersonate all users.
65
+
-`list of users`: Grants permission to impersonate specific users (comma-separated list).
66
+
-`user/role`: The user or role receiving the impersonation permission.
67
+
68
+
Here is an example of granting the `admin` role permission to impersonate `user1` and `user2`:
69
+
```cypher
70
+
GRANT IMPERSONATE_USER user1,user2 TO admin;
71
+
```
72
+
73
+
Here is an example of granting the `admin_user` user permission to impersonate all users:
74
+
```cypher
75
+
GRANT IMPERSONATE_USER * TO admin_user;
76
+
```
77
+
78
+
### Deny impersonate user
79
+
80
+
The `DENY IMPERSONATE_USER` denies impersonation rights to specific users or roles, allowing you to restrict who can impersonate whom.
81
+
The syntax and behavior are as follows:
82
+
83
+
```cypher
84
+
DENY IMPERSONATE_USER list of users TO user/role;
85
+
```
86
+
87
+
Here is the explanation of arguments:
88
+
-`list of users`: Deny impersonation for specific users (comma-separated list).
89
+
-`user/role`: The user or role being restricted from impersonating others.
90
+
91
+
Here is an example of denying the `admin` role the ability to impersonate `user1` and `user2`:
92
+
```cypher
93
+
DENY IMPERSONATE_USER user1,user2 TO admin;
94
+
```
95
+
96
+
### Revoke impersonate user
97
+
98
+
The `REVOKE IMPERSONATE_USER` removes the impersonation rights for a given user or role. It revokes all impersonation permissions for the specified user/role.
99
+
The syntax and behavior are as follows:
100
+
101
+
```cypher
102
+
REVOKE IMPERSONATE_USER FROM user/role;
103
+
```
104
+
105
+
Here is the explanation of arguments:
106
+
-`user/role`: The user or role whose impersonation permissions are being revoked.
107
+
108
+
Here is an example of revoking all impersonation permissions for the `admin` role:
109
+
```cypher
110
+
REVOKE IMPERSONATE_USER FROM admin;
111
+
```
112
+
113
+
<Callouttype="info">
114
+
**Important things to note**
115
+
116
+
When using the `GRANT` or `DENY` commands, you must provide exhaustive lists of users. This means that the existing configuration will be replaced by the new list provided.
117
+
For example:
118
+
- First command:
119
+
```cypher
120
+
GRANT IMPERSONATE_USER user1,user2 TO admin;
121
+
```
122
+
- Second command (this overrides the first one):
123
+
```cypher
124
+
GRANT IMPERSONATE_USER user3 TO admin;
125
+
```
126
+
After the second command, the `admin` role will only be able to impersonate `user3`, even though the first command allowed impersonation of `user1` and `user2`.
127
+
128
+
Permissions can be granted or denied to individual users or roles. For example, an `admin` role might have impersonation privileges that individual users do not have.
129
+
The `REVOKE` command removes any impersonation permissions for the specified user or role, ensuring that they cannot impersonate any user unless granted explicitly again.
0 commit comments