# Adding postgres datasource using API

**URL:** <https://community.grafana.com/t/adding-postgres-datasource-using-api/120961>\
**Category:** PostgreSQL\
**Created:** [May 5, 2024, 9:20am UTC](https://community.grafana.com/t/adding-postgres-datasource-using-api/120961 "2024-05-05T09:20:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![balureddyora](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@balureddyora](https://community.grafana.com/u/balureddyora)\
**Post date:** [May 5, 2024, 9:20am UTC](https://community.grafana.com/t/adding-postgres-datasource-using-api/120961/1 "2024-05-05T09:20:55Z")

</div>

Hi All,  
I am trying add postgres datasource using ansible and task looks like below.

- name: Add PostgreSQL data source  
uri:  
url: “{{ grafana\_url }}/api/datasources”  
method: POST  
body\_format: json  
body:  
name: ‘PostgreSQL’  
type: ‘postgres’  
url: ‘localhost:5432’  
access: ‘proxy’  
database: ‘your\_database’  
user: ‘your\_username’  
secureJsonData:  
password: ‘your\_password’  
isDefault: true  
headers:  
Authorization: “Bearer {{ token }}”  
Content-Type: “application/json”  
return\_content: yes  
status\_code: 200

Eventhough the datasource getting added but type shows undefined, when i open settings, i can see only Delete option. Can someone help me to identify what could be the issue and i am using grafana 10.4 OSS.

---

<div class="post-metadata">

**Author:** ![sowdenraymond](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/sowdenraymond/32/38541_2.png) [@sowdenraymond](https://community.grafana.com/u/sowdenraymond)\
**Post date:** [May 6, 2024, 9:05am UTC](https://community.grafana.com/t/adding-postgres-datasource-using-api/120961/2 "2024-05-06T09:05:10Z")

</div>

I think type should be PostgreSQL?

![image](https://us1.discourse-cdn.com/grafana/original/3X/f/3/f345acc7fd715bc5a5d722cf61f88bb4be4ace95.png)

---

<div class="post-metadata">

**Author:** ![balureddyora](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@balureddyora](https://community.grafana.com/u/balureddyora)\
**Post date:** [May 6, 2024, 1:13pm UTC](https://community.grafana.com/t/adding-postgres-datasource-using-api/120961/3 "2024-05-06T13:13:53Z")

</div>

I tried postgresql as well, still same error. Anything mistake I am doing in playbook above and by the way I am using poatgres 10.4.0 version.

---

<div class="post-metadata">

**Author:** ![balureddyora](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@balureddyora](https://community.grafana.com/u/balureddyora)\
**Post date:** [May 6, 2024, 5:30pm UTC](https://community.grafana.com/t/adding-postgres-datasource-using-api/120961/4 "2024-05-06T17:30:30Z")

</div>

I went with below approach and it worked and also i went with grafana version to 10.0.0. since community.grafana.grafana\_datasource tested till 10.2.2 i think.

- name: Create postgres datasource  
community.grafana.grafana\_datasource:  
name: “PostgreSQL”  
grafana\_url: “{{ grafana\_url }}”  
grafana\_user: “{{ grafana\_admin\_user }}”  
grafana\_password: “{{ grafana\_admin\_password }}”  
#org\_id: “1”  
ds\_type: “postgres”  
ds\_url: “{{ postgres\_url }}”  
database: “{{ db\_name }}”  
user: “{{ db\_owner }}”  
sslmode: “disable”  
additional\_json\_data:  
postgresVersion: 15  
timescaledb: false  
additional\_secure\_json\_data:  
password: “{{ db\_password }}”
