Movatterモバイル変換


[0]ホーム

URL:


You don’t need a vector database - just use Postgres for everything. Read the case study on switching from Pinecone to Neon
PostgreSQL Tutorial
PostgreSQL Tutorial

PostgreSQL PI() Function

Summary: in this tutorial, you will learn how to use the PostgreSQLPI() function to return the pi value.

Introduction to the PostgreSQL PI() function

In PostgreSQL, thePI() function returns the value of pi denoted by the Greek letter (π), which is approximately equal to3.14

Here’s the syntax of thePI() function:

PI()

ThePI() function takes no arguments and returns the constant value ofPI, which is3.141592653589793.

PostgreSQL PI() function examples

Let’s take some examples of using thePI() function examples.

1) Basic PI() function examples

The following statement uses thePI() function to return the constantPI value:

SELECT PI();

Output:

pi------------------- 3.141592653589793

The following example uses thePI() function to calculate the area of a circle with a radius of 10:

SELECT PI()* 10 * 10 area;

Output:

area------------------- 314.1592653589793(1 row)

2) Using the PI() function with table data

First,create a table calledcircles that stores the radiuses of circles:

CREATE TABLE circles(   idINT GENERATED ALWAYS AS IDENTITY,   radiusDEC(19,2)NOT NULL,   PRIMARY KEY(id));

Second,insert rows into thecircles table:

INSERT INTO circles(radius)VALUES(10), (20), (25)RETURNING*;

Output:

id | radius----+--------  1 |  10.00  2 |  20.00  3 |  25.00(3 rows)

Third, calculate the areas of circles using thePI() function:

SELECT id, radius,PI()* radius* radius areaFROM circles;

Output:

id | radius |        area----+--------+--------------------  1 |  10.00 |  314.1592653589793  2 |  20.00 | 1256.6370614359173  3 |  25.00 | 1963.4954084936207(3 rows)

To make the area more readable, you can use theROUND() function:

SELECT  id,  RADIUS,  ROUND((PI()* RADIUS* RADIUS)::NUMERIC,2) AREAFROM  circles;

Output:

id | radius |  area----+--------+---------  1 |  10.00 |  314.16  2 |  20.00 | 1256.64  3 |  25.00 | 1963.50(3 rows)

Summary

  • Use thePI() function to return the pi value.

Last updated on

Was this page helpful?
Thank you for your feedback!

[8]ページ先頭

©2009-2025 Movatter.jp