Before exporting data, you must:

Grant the Database Service export service account write access to
the destination object storage bucket. This role binding must be created within the same namespace as the subject database cluster. See [Grant and obtain storage access](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform/pa-user/grant-obtain-storage-access.md.txt#grant_bucket_access).

The Database Service system automatically creates this service account when the database cluster is created. To find the namespace that contains the controller-maintained service account, use the following commands:

```bash
kubectl get dbclusters.postgresql <var>DATABASE_CLUSTER_NAME</var> -n <var>PROJECT_NAMESPACE</var> -o jsonpath='{.status.serviceAccounts.export}'

kubectl get dbclusters.postgresql <var>DATABASE_CLUSTER_NAME</var> -n <var>PROJECT_NAMESPACE</var> -o jsonpath='{.status.serviceAccounts.import}'
```

Once you have the namespace, the service account you're looking for is named
`postgresql-export-DATABASE_CLUSTER_NAME`.

Replace `DATABASE_CLUSTER_NAME` with the name of the
database cluster where you are exporting data.

You can export a database cluster to a data dump file using either the
GDC console or the Distributed Cloud CLI:

### Console

1. From the main menu, choose **Database Service**.
2. Select the database cluster you want to export. This takes you to the **Database cluster overview** page for that cluster.
3. Click **EXPORT** . The **Export data** panel opens.
4. In the **Export data** panel, specify the storage location to export to.
5. Click **EXPORT**. On screen messages indicate the status of the export process.

### gdcloud

1. Before using the gdcloud CLI, [install and initialize](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/resources/gdcloud-install.md.txt) it. Then, [authenticate](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/resources/gdcloud-auth.md.txt) with your organization.
2. Run the following command to export a database file to a dump file:

   ```bash
   gdcloud database export sql DATABASE_CLUSTER \
        BUCKET_NAME/SAMPLE.dmp --project=PROJECT_NAME
   ```

   Replace the following:
   - <var translate="no">DATABASE_CLUSTER</var> with the name of the database cluster to export.
   - <var translate="no">BUCKET_NAME/SAMPLE.dmp</var> with the destination for the exported dump file.
   - <var translate="no">PROJECT_NAME</var> with the name of the project that the database cluster is in.

### API

```yaml
apiVersion: postgresql.dbadmin.gdc.goog/v1
kind: Export
metadata:
  name: EXPORT_NAME
  namespace: USER_PROJECT
spec:
  dbclusterRef: DBCLUSTER_NAME
  exportLocation:
    s3Options:
      bucket: BUCKET_NAME
      key: dbs-export
    type: S3
```

Replace the following variables:

- `EXPORT_NAME`: the name of the export operation.
- `USER_PROJECT`: the name of the user project where the database cluster to be exported is created.
- `DBCLUSTER_NAME`: the name of the database cluster.
- `BUCKET_NAME`: the name of the object storage bucket to store the exported files.