Important
La traduction est le fruit d’un effort communautaire auquel vous pouvez vous joindre. Cette page est actuellement traduite à 73.68%.
16.3. Leçon : Importer et Exporter
Of course, a database with no easy way to migrate data into it and out of it would not be of much use. Fortunately, there are a number of tools that will let you easily move data into and out of PostgreSQL.
16.3.1. shp2pgsql
shp2pgsql is a commandline tool to import ESRI Shapefile to the database. Under Unix, you can use the following command for importing a new PostgreSQL table:
shp2pgsql -s <SRID> -c -D -I <path to shapefile> <schema>.<table> | \
psql -d <databasename> -h <hostname> -U <username>
Sous Windows, vous devez réaliser le processus d’importation en deux étapes:
shp2pgsql -s <SRID> -c -D -I <path to shapefile> <schema>.<table> > import.sql
psql psql -d <databasename> -h <hostname> -U <username> -f import.sql
Vous pouvez rencontrer cette erreur:
ERROR: operator class "gist_geometry_ops" does not exist for access method
"gist"
C’est un problème connu lié à la création in situ d’un index spatial sur la couche en cours d’importation. Pour éviter l’erreur, excluez le paramètre -I. Cela signifie qu’aucun index spatial ne sera directement créé sur la table, et que vous aurez besoin de le créer une fois la donnée importée dans la base de données. (La création d’un index spatial sera abordée dans la prochaine leçon.)
16.3.2. pgsql2shp
pgsql2shp is a commandline tool to export PostgreSQL Tables, Views or SQL select queries. To do this under Unix:
pgsql2shp -f <path to new shapefile> -g <geometry column name> \
-h <hostname> -U <username> <databasename> <table | view>
Pour exporter des données à l’aide d’une requête:
pgsql2shp -f <path to new shapefile> -g <geometry column name> \
-h <hostname> -U <username> "<query>"
16.3.3. ogr2ogr
ogr2ogr is a very powerful tool to convert data into and from PostgreSQL to many data formats. ogr2ogr is part of the GDAL library and has to be installed separately. To export a table from PostgreSQL to GML, you can use this command:
ogr2ogr -f GML export.gml PG:'dbname=<databasename> user=<username>
host=<hostname>' <Name of PostgreSQL-Table>
16.3.4. Le Gestionnaire de Bases de Données
You may have noticed another option in the Database menu labeled DB Manager. This is a tool that provides a unified interface for interacting with spatial databases including PostgreSQL. It also allows you to import and export from databases to other formats. Since the next module is largely devoted to using this tool, we will only briefly mention it here.
16.3.5. Conclusion
Importer et exporter vers et depuis une base de données peut être fait de diverses manières. Vous utiliserez probablement ces fonctions (ou des similaires) si vous utilisez des données de sources disparates.
16.3.6. La suite ?
Prochainement, nous verrons comment interroger les données que nous avons créées auparavant.