> For the complete documentation index, see [llms.txt](https://academy.cegedim.cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://academy.cegedim.cloud/databases/postgresql/postgresql-get-started.md).

# PostgreSQL - Get started

## How to provision PostgreSQL? <a href="#postgresqlhowtos-howtoprovisionpostgresql" id="postgresqlhowtos-howtoprovisionpostgresql"></a>

{% stepper %}
{% step %}

### Open your Global Service

In ITCare, search your target Global Service in the top search bar and click on it to display its information page.
{% endstep %}

{% step %}

### Select PostgreSQL

Click the **Create a resource** button, go to **Managed databases** and select **PostgreSQL**. Pick the required version.
{% endstep %}

{% step %}

### Fill in the form

Fill in the form then click **Next**. Select your customizations and click **Next**.
{% endstep %}

{% step %}

### Submit the request

Review the synthesis before submitting the form.
{% endstep %}
{% endstepper %}

{% hint style="info" %}
Provisioning can take up to 2 hours based on the current load on automation.
{% endhint %}

Once the deployment is ready, you will be notified by email.

## How to manage your PostgreSQL?

On the resource page of your PostgreSQL, you can take any action available by using the Manage button in the upper right corner. This includes, starting, stopping, deleting, rebooting, resizing and much more.

## How to access your PostgreSQL deployment? <a href="#postgresqlhowtos-howtoaccessyourpostgresqlcluster" id="postgresqlhowtos-howtoaccessyourpostgresqlcluster"></a>

When your deployment is created with **cegedim.cloud** ITCare, you obtained a SQL role named "admin" with credentials.

With these credentials, you may connect to the deployment with its name on tcp port 5432. You may use the postgres database to connect to.

If your deployment is named "mycluster", here is an example on how to connect using Python:

{% tabs %}
{% tab title="Without TLS" %}
{% code lineNumbers="true" %}

```python
import psycopg2

# Connect to the postgres database
conn = psycopg2.connect(database='postgres', user='myuser', password='mystrongpwd', host='mycluster.pg.cegedim.cloud')
# Open a cursor to perform database operations
cursor = conn.cursor()
# Execute a query
cursor.execute("SELECT datname from pg_database")
# Retrieve query results
records = cursor.fetchall()
# Print all results
for record in records:
  print(record)
```

{% endcode %}
{% endtab %}

{% tab title="With TLS/SSL" %}
If you choose to activate TLS, you have received the root certificate you should trust and give to the library you use to connect, for example psycopg2.

{% code lineNumbers="true" %}

```python
import psycopg2

# Connect to the postgres database
conn = psycopg2.connect(database='postgres', user='myuser', password='mystrongpwd', host='mycluster.pg.cegedim.cloud', sslmode='verify-full', sslrootcert='cegedimcloud.pg.crt')
# Open a cursor to perform database operations
cursor = conn.cursor()
# Execute a query
cursor.execute("SELECT datname from pg_database")
# Retrieve query results
records = cursor.fetchall()
# Print all results
for record in records:
  print(record)
```

{% endcode %}
{% endtab %}
{% endtabs %}

## How to create a role? <a href="#postgresqlhowtos-howtocreatearole" id="postgresqlhowtos-howtocreatearole"></a>

{% hint style="info" %}
It is safer not to use the admin role for applications.
{% endhint %}

Once connected, you may create a regular role as the following (replace \<a\_role> and \<very\_strong\_password> with your own credentials)

```sql
create role <a_role> login password '<very_strong_password>';
```

## How to create a database? <a href="#postgresqlhowtos-howtocreateadatabase" id="postgresqlhowtos-howtocreateadatabase"></a>

If you want to create a database owned by the role you have just created, use the following SQL statements:

```sql
grant <a_role> to admin;
create database <my_database> owner <a_role>;
```

## How to create a database with another encoding and/or collation? <a href="#postgresqlhowtos-howtocreateadatabasewithanotherencodingand-orcollation" id="postgresqlhowtos-howtocreateadatabasewithanotherencodingand-orcollation"></a>

You may use the following SQL requests with template0 database as template database:

{% code overflow="wrap" %}

```sql
create database <my_database> owner <a_role> template template0 LC_COLLATE 'fr_FR.utf8';
create database <my_database> owner <a_role> template template0 encoding 'LATIN1' LC_COLLATE 'fr_FR';
```

{% endcode %}

## How to restore in self-service? <a href="#postgresqlhowtos-howtorestoreinself-service" id="postgresqlhowtos-howtorestoreinself-service"></a>

The PostgreSQL PaaS has a functionality allowing to restore a PostgreSQL PaaS (source) to another PostgreSQL PaaS (destination) at a given time (using Point-In-Time Recovery) under the following constraints:

* the user must have access to the cloud of the source deployment and the destination deployment
* the source must be backed up (option chosen during creation)
* both source and destination must be active
* the source and destination must be different
* the source and destination must be in the same version of PostgreSQL
* the source and destination must be in version 12 or higher
* the target time must not be in the future (bounded on the right by the current time).
* the target time must not be more than 7 days (for non-production services) or 14 days (for production services) in the past (bounded on the left by the retention of backups)

You can choose to include or exclude the time target in the restoration process.

## How to install the oracle\_fdw extension? <a href="#postgresqlhowtos-howtoinstall-oracle-fdw" id="postgresqlhowtos-howtoinstall-oracle-fdw"></a>

You can install the oracle\_fdw extension on your databases. This extension allows you to connect to a PostgreSQL deployment and read/write tables that are actually located on an Oracle server. To do this, using ITCare, go to the Manage menu, then Manage Extensions, select your database, then oracle\_fdw, and click the Submit button. Remember that according to best practices, you have created a role and a dedicated database:

```sql
create role myrole login;
alter role myrole password '********';
grant myrole to admin;
create database customer owner myrole;
```

Once the extension is installed, you must create several objects in the database. You must have the following items to perform this operation:

* the username and password for the account (admin) that was provided to you when you created your PostgreSQL deployment.
* the role that will connect to the PostgreSQL database and access the Oracle database
* The SID of the Oracle database
* The username and password for the role that has the right to connect to the Oracle database and read/write the tables in question.
* The name and definition of the Oracle tables

<details>

<summary>Example: the Oracle table used below</summary>

For the rest of this example, we will assume that a table has been created in Oracle as follows:

```sql
CREATE TABLE person (
  id NUMBER(10, 0) PRIMARY KEY,
  name VARCHAR2(64),
  ts TIMESTAMP
);
```

</details>

Using your admin account on the customer database, you must create a mapping between your Oracle server, the Oracle database, and the Oracle role (the role that created the person table, for example)

```sql
CREATE SERVER oracle_server FOREIGN DATA WRAPPER oracle_fdw OPTIONS (dbserver '//myora.hosting.cegedim.cloud:1521/mysid');
CREATE USER MAPPING FOR myrole SERVER oracle_server OPTIONS (user 'orauser', password 'mypwd');
GRANT USAGE on foreign server oracle_server to myrole;
```

Finally, with the myrole role, connected to the customer database (in PostgreSQL):

```sql
CREATE FOREIGN TABLE person (
    id BIGINT OPTIONS (key 'true') NOT NULL,
    name VARCHAR(64),
    ts TIMESTAMP WITHOUT TIME ZONE
)
SERVER oracle_server
OPTIONS (table 'PERSON');
select * from person;
 id |   name    |             ts            
----+-----------+----------------------------
  0 | Asterix   | 2025-10-08 09:17:26.591058
  1 | Obelix    | 2025-10-08 09:17:34.893623
  2 | Panoramix | 2025-10-08 09:17:47.758639
  3 | Idefix    | 2025-10-08 09:17:58.063794
(4 rows)
```

<details>

<summary>Fix an incorrect server or user mapping</summary>

If an error occurs when declaring the correspondences between the server and the Oracle role, the admin role can make the following changes:

```sql
-- modify the foreign server retrospectively if a parameter has been entered incorrectly
alter server oracle_server OPTIONS (SET dbserver '//so19cust03.hosting.cegedim.cloud:1521/SO19CUSTO3');
-- modify a user mapping
ALTER USER MAPPING FOR myrole SERVER oracle_server OPTIONS (SET password 'theGoodPassword');
```

</details>

The documentation for the oracle\_fdw extension is available [here](https://github.com/laurenz/oracle_fdw).

## How to install the postgres\_fdw extension? <a href="#postgresqlhowtos-howtoinstall-postgres-fdw" id="postgresqlhowtos-howtoinstall-postgres-fdw"></a>

You can install the postgres\_fdw extension on your databases. This extension allows you to read/write tables located in another PostgreSQL deployment. To do this, using ITCare, go to the Manage menu, then Manage Extensions on your pg16 deployment, select your database, then postgres\_fdw, and click the Submit button. Remember that according to best practices, you have created a role and a dedicated database on each of the PostgreSQL deployments:

<details>

<summary>Example: create the test data on pg15</summary>

On the pg15 server in version 15, for example (pg15.pg.cegedim.cloud)

```sql
create role rolea login;
alter role rolea password '***********';
grant rolea to admin;
create database mydb owner rolea;
\c mydb
CREATE TABLE mytable (
    id INTEGER PRIMARY KEY,
    ts TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
alter table mytable owner rolea;
insert into mytable values(0);
insert into mytable values(1);
insert into mytable values(2);
insert into mytable values(3);
select * from mytable;
 id |              ts              
----+-------------------------------
  0 | 2025-10-07 09:00:47.607772+00
  1 | 2025-10-07 09:00:50.998144+00
  2 | 2025-10-07 09:00:53.639238+00
  3 | 2025-10-07 09:00:56.134559+00
(4 rows)
```

</details>

On the pg16 server (pg16.pg.cegedim.cloud):

```sql
create role roleb login;
alter role roleb password '**********';
grant roleb to admin;
create database customer owner roleb;
```

With the admin role on the customer database of the pg16 deployment, create the mappings with the pg15 server and the rolea role:

```sql
create server pg15 foreign data wrapper postgres_fdw options (host 'pg15.pg.cegedim.cloud', dbname 'mydb');
create user mapping for roleb server pg15 options(user 'rolea', password '******'); -- use the password for the rolea role on the pg15 server
grant usage on foreign server pg15 to roleb;
```

With the roleb role on the pg16 deployment, customer database, import the definition of the mytable table:

```sql
-- import the definition of the mytable table from pg15
IMPORT FOREIGN SCHEMA public LIMIT TO (mytable) FROM SERVER pg15 INTO public;
-- the table is now accessible:
 select * from mytable;
 id |              ts              
----+------------------------- ------
  0 | 2025-10-07 09:00:47.607772+00
  1 | 2025-10-07 09:00:50.998144+00
  2 | 2025-10-07 09:00:53.639238+00
  3 | 2025-10-07 09:00:56.134559+00
(4 rows)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://academy.cegedim.cloud/databases/postgresql/postgresql-get-started.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
