PostgreSQLとPostGISのバージョンを取得する方法
SQLコマンドを使用する
PostgreSQLとPostGISのバージョンを取得するには、以下のSQLコマンドを使用できます。
SELECT version();
このコマンドは、PostgreSQLサーバーのバージョンとPostGISのバージョンを含む、PostgreSQLサーバーに関する情報を表示します。
# PostgreSQL 15.4とPostGIS 3.2.2の場合
postgres=# SELECT version();
version
-----------------------------------------------------------------------------------------------------
PostgreSQL 15.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.2.0, 64-bit
This is a development version of PostgreSQL.
The full version string is:
PostgreSQL 15.4devel (Debian 15.4-1) on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.2.0, 64-bit
...
PostGIS Geometry Version: 3.2.2-1
SELECT postgis_full_version();
このコマンドは、PostGISのバージョンと、PostGISに依存するライブラリのバージョンを表示します。
# PostgreSQL 15.4とPostGIS 3.2.2の場合
postgres=# SELECT postgis_full_version();
postgis_full_version
--------------------------------------------------------------------------------------
POSTGIS="3.2.2" [EXTENSION] PGSQL="150" GEOS="3.10.2" PROJ="8.2.1" GDAL="3.4.2" LIBXML="2.9.12" LIBJSON="0.13.1" TOPOLOGY="1.5.2"
プログラミング言語を使用する
以下の例は、Pythonを使用してPostgreSQLとPostGISのバージョンを取得する方法を示しています。
import psycopg2
# PostgreSQLサーバーに接続
connection = psycopg2.connect(
database="postgres",
user="postgres",
password="password",
host="localhost",
port="5432",
)
# PostgreSQLサーバーのバージョンを取得
cursor = connection.cursor()
cursor.execute("SELECT version()")
version = cursor.fetchone()[0]
# PostGISのバージョンを取得
cursor.execute("SELECT postgis_full_version()")
postgis_version = cursor.fetchone()[0]
# 結果を出力
print("PostgreSQLバージョン:", version)
print("PostGISバージョン:", postgis_version)
# 接続を閉じる
cursor.close()
connection.close()
この例では、psycopg2
ライブラリを使用してPostgreSQLサーバーに接続しています。
他のプログラミング言語でも、PostgreSQLとPostGISに接続できるライブラリを使用すれば、同様にバージョンを取得することができます。
- 簡単にバージョンを確認したい場合は、SQLコマンドを使用するのが便利です。
- プログラムの中でバージョンを取得したい場合は、プログラミング言語を使用する必要があります。
PostgreSQLとPostGISのバージョンを取得するサンプルコード
SQLコマンドを使用する
-- PostgreSQLサーバーとPostGISのバージョンを取得
SELECT version();
-- PostGISのバージョンのみを取得
SELECT postgis_full_version();
Pythonを使用する
import psycopg2
# PostgreSQLサーバーに接続
connection = psycopg2.connect(
database="postgres",
user="postgres",
password="password",
host="localhost",
port="5432",
)
# PostgreSQLサーバーのバージョンを取得
cursor = connection.cursor()
cursor.execute("SELECT version()")
version = cursor.fetchone()[0]
# PostGISのバージョンを取得
cursor.execute("SELECT postgis_full_version()")
postgis_version = cursor.fetchone()[0]
# 結果を出力
print("PostgreSQLバージョン:", version)
print("PostGISバージョン:", postgis_version)
# 接続を閉じる
cursor.close()
connection.close()
Javaを使用する
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class GetPostGISVersion {
public static void main(String[] args) throws Exception {
// PostgreSQLサーバーに接続
Connection connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/postgres",
"postgres",
"password"
);
// PostgreSQLサーバーのバージョンを取得
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT version()");
String version = resultSet.getString(1);
// PostGISのバージョンを取得
resultSet = statement.executeQuery("SELECT postgis_full_version()");
String postgisVersion = resultSet.getString(1);
// 結果を出力
System.out.println("PostgreSQLバージョン: " + version);
System.out.println("PostGISバージョン: " + postgisVersion);
// 接続を閉じる
resultSet.close();
statement.close();
connection.close();
}
}
C#を使用する
using System;
using System.Data.Common;
using Npgsql;
public class GetPostGISVersion {
public static void Main(string[] args) {
// PostgreSQLサーバーに接続
using (NpgsqlConnection connection = new NpgsqlConnection("Server=localhost;Port=5432;Database=postgres;User Id=postgres;Password=password")) {
connection.Open();
// PostgreSQLサーバーのバージョンを取得
using (NpgsqlCommand command = new NpgsqlCommand("SELECT version()", connection)) {
string version = (string)command.ExecuteScalar();
// PostGISのバージョンを取得
command.CommandText = "SELECT postgis_full_version()";
string postgisVersion = (string)command.ExecuteScalar();
// 結果を出力
Console.WriteLine("PostgreSQLバージョン: " + version);
Console.WriteLine("PostGISバージョン: " + postgisVersion);
}
}
}
}
PostgreSQLとPostGISのバージョンを取得するその他の方法
PostgreSQLの公式ドキュメントには、PostgreSQLとPostGISのバージョンを取得する方法に関する情報が掲載されています。
GUIツールを使用する
PostgreSQLとPostGISを管理するためのGUIツールを使用することもできます。
これらのツールには、PostgreSQLとPostGISのバージョン情報を含む、PostgreSQLサーバーに関する情報が表示される機能が通常備わっています。
コマンドラインツールを使用する
psql
postgres
shp2pgsql
- GUIツールやコマンドラインツールを使用するのも一つの方法です。
postgresql postgis