PostgreSQLとPostGISのバージョンを取得する方法

2024-04-02

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


PostgreSQL: PL/pgSQLを使用してデータをCSVファイルに書き出す

PostgreSQLデータベースへのアクセス基本的なPL/pgSQLの知識PL/pgSQL関数を作成するPL/pgSQL関数を作成する上記コードの変更点:filename 変数を、実際のファイルパスに変更します。header 変数は、出力するCSVファイルのヘッダー行の内容に変更します。...


PostgreSQLで文字列を整数に変換する方法

しかし、文字列が数値に変換できない場合、エラーが発生します。エラー発生時に 0 を返すには、COALESCE() 関数と組み合わせて使用します。COALESCE() 関数は、最初の引数が NULL または空の場合、2番目の引数を返します。CAST() 関数以外にも、文字列を整数に変換する方法はいくつかあります。...


【Mac】Ruby on RailsでPostgreSQLを使うには?インストールから基本操作まで徹底解説

原因: この問題は、いくつかの要因が考えられます。Homebrew のインストール: Homebrew を使用して PostgreSQL をインストールしている場合、Homebrew 独自のバージョンがインストールされ、RubyGems と互換性が無い可能性があります。...


PostgreSQLデータベースのバックアップと復元の重要性

データベースのバックアップを作成するには、pg_dumpコマンドを使用します。このコマンドは、データベースとそのすべてのスキーマ、データ、および拡張機能を単一のファイルにダンプします。上記の例では、postgresユーザーとしてmydbaseデータベースのバックアップを作成し、mydbase...


pg_terminate_backendコマンドでPostgreSQLのクエリを強制終了する

方法1: pg_terminate_backendコマンドを使用するpg_terminate_backendコマンドは、特定のバックエンドプロセスを強制終了するために使用されます。このコマンドを使用するには、以下の情報を取得する必要があります。...