Postgresql db import not working well - postgresql

I am trying to import a sql dump to postgresql db as -
sudo su postgres -c psql dbname < mydb_dump.sql
This gives errors as -
SET
SET
SET
SET
SET
SET
ERROR: function "array_accum" already exists with same argument types
ALTER AGGREGATE
ERROR: function "c" already exists with same argument types
ALTER AGGREGATE
ERROR: duplicate key value violates unique constraint "pg_ts_config_cfgname_index"
ERROR: duplicate key value violates unique constraint "pg_ts_config_map_index"
and so on this. What might be wrong with that? Tried googling for it, but couldn't find any pointers over it.
Postgresql version is 8.4.1
Thanks !!

you should to remove shared functions and objects from database, before you do dump or before you load dump. You have these functions and objects registered in template1, and when you create new database, then these objects are there - and you can see errors when dump try to create it again.
This issue is well solved in PostgreSQL 9.1. For older versions try to use option --clean for pg_dump
Pavel

Related

copying postgreSQL database, table with same number of rows, but different table size

I dumped the database with:
pg_dump dsmartgpp -f /data1/master/backup/db.sql
and I copied the database with
nohup psql -h gpnm1 -d dsmartgpp_1 -U gpadmin < /data1/master/backup/db.sql
the log information showed some errors about the function and datatype of postgis, such as
ALTER FUNCTION
ERROR: function "gidx_out" already exists with same argument types.
ALTER FUNCTION
ERROR: type "gidx" already exists
some tables between the two database have the same number or records, but about 1MB difference in size.
That is as expected. Restoring a pg_dump creates a logical copy of the database. In particular, the copy won't be bloated (don't worry – a bit of bloat is healthy).
If you want to avoid these errors, create the new database (dsmartgpp_1) with template0 as template (empty database). You seem to have PostGIS installed in template1, which gets copied into every new database. Or you installed PostGIS in your new database, before importing the dump.
Either way, create the empty database (dsmartgpp_1) and let the dump install the PostGIS functions.
Oh, one more thing, you can use "-f /data1/master/backup/db.sql" instead of the "<" redirect.
If you want to be super careful, also add "-v ON_ERROR_STOP=1". This will stop at the first error.

How to solve extensions issues when restore PostgreSQL Database

I'm restoring a .sql Postgres dump on a database I created with
CREATE DATABASE my_database;
Later When I restore the database with
psql my_database < dump.sql
However, I encountered several errors which prevents me from restoring the data:
ERROR: schema "pglogical" already exists
ALTER SCHEMA
CREATE EXTENSION
COMMENT
ERROR: could not open extension control file "/usr/share/postgresql/11/extension/pglogical.control": No such file or directory
ERROR: extension "pglogical" does not exist
ERROR: could not open extension control file "/usr/share/postgresql/11/extension/postgis.control": No such file or directory
ERROR: extension "postgis" does not exist
ERROR: function "order_sorting_weight" already exists with same argument types
ERROR: type "public.geometry[]" does not exist
LINE 7: viewer_locations public.geometry(Point,4326)[]
Following by repeatedly reporting:
ERROR: relation "auth_permission_id_seq" already exists
ALTER TABLE
ALTER SEQUENCE
ERROR: relation "auth_user" already exists
ALTER TABLE
ERROR: relation "auth_user_groups" already exists
ALTER TABLE
ERROR: relation "auth_user_groups_id_seq" already exists
for each column of the db I guess.
How could I solve this issue?
OK, I solved the problem JUST by changing the version of posters I've been using, as I installed the latest version of Postgres (11.1), I couldn't restore the .sql dump files to it, and the dump file was getting dumped from a database server with version 9.5. so I just uninstalled the whole Postgresql and installed the 9.5 version of it. I setup pglogical extension with help of plogical-docs and now everything works just as I wanted.

ERROR: function addgeometrycolumn(unknown..), when inserting sql file in to postgis database

I am trying to import the spatial file in to my database
Firstly i have created a database using postgis template as below
createdb -T template_postgis database_name;
I have postgis installed already on my machine
POSTGIS="2.1.1 r12113" GEOS="3.3.3-CAPI-1.7.4" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 1.9.0, released 2011/12/29" LIBXML="2.7.8" LIBJSON="UNKNOWN" TOPOLOGY RASTER
Postgres version : psql (9.3.2, server 9.1.11)
Secondly i have converted the spatial file in to sql file as below
shp2pgsql -s 3425 Aspire.shp test_for_shape_data database_name > shapefile_data.sql
And now i am trying to import this sql file(shapefile_data.sql) in to my database(database_name) like below
psql -d database_name username -f shapefile_data.sql
But i am getting the following error
user#user:~/user/spice$ psql -d psql -d database_name username -f shapefile_data.sql
SET
SET
BEGIN
psql:shapefile_data.sql:30: NOTICE: CREATE TABLE will create implicit sequence "test_for_shape_data_gid_seq" for serial column "test_for_shape_data.gid"
CREATE TABLE
psql:shapefile_data.sql:31: NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "test_for_shape_data_pkey" for table "test_for_shape_data"
ALTER TABLE
psql:shapefile_data.sql:32: ERROR: function addgeometrycolumn(unknown, unknown, unknown, unknown, unknown, integer) is not unique
LINE 1: SELECT AddGeometryColumn('','test_for_shape_data','geom','42...
^
HINT: Could not choose a best candidate function. You might need to add explicit type casts.
psql:shapefile_data.sql:33: ERROR: current transaction is aborted, commands ignored until end of transaction block
psql:shapefile_data.sql:34: ERROR: current transaction is aborted, commands ignored until end of transaction block
...........
.......
Why it is telling me that i don't have AddGeometryColumn function even though i have created the database with postgis template ?
So how to avoid this ?
Also when i try to manually enable the postgis functions its telling that already exists
database_name=# CREATE EXTENSION postgis;
ERROR: type "spheroid" already exists
database_name=# CREATE EXTENSION postgis_topology;
ERROR: required extension "postgis" is not installed
So how to clear this error and insert the shapefile sql file in to database ?
It sounds like you've got an old PostGIS install that was created from an SQL script, pre-extension support. It probably only has some of the functionality and features you expect. This sort of behaviour can occur when you've got a PostGIS 1.5 schema and a PostGIS 2.0 install, etc.
Try creating your DB from template0 instead, and running CREATE EXTENSION postgis; then doing a restore. I suspect your template_postgis contains an old version of the extension schema.
See the PostGIS upgrade guide.

Enable PostGIS extension error

I have PostGIS 2.0.4 installed with PostgreSQL 9.2.4, trying to enable spatial database extension with the following command:
CREATE EXTENSION postgis;
but encountered an error -
ERROR: must be owner of type spheroid
What is this spheroid type? How could I enable it?
Update:
Well, I still couldn't find a solution so I had to remove everything and reinstall again. Now I am getting a new error saying:
ERROR: could not load library "/usr/pgsql-9.2/lib/rtpostgis-2.0.so": libclntsh.so.11.1 cannot open shared object file: No such file or directory.
I have no luck google out an answer to this. It seems libclntsh.so.11.1 is a Oracle library? Why it has something to do with PostgreSQL?
Try logging in as a db superuser. On Linux the following should work on most distros:
sudo postgres psql [dbname]
Then ownership and permissions can be effectively ignored. In general I would recommend doing this for most extension installations as this usually requires superuser privileges anyway.
I know this is an ancient question, but I want an answer here for the next time I run into it!
Drop the tables spatial_ref_sys and geometry_columns. They shouldn't be tables in your schema. In my case, they got created as an artefact of using DotNet to create Entity Framework models from a SQL Server database, then using EF to recreate the DB in Postgres.
So:
sudo postgresql psql [dbname] -c "drop table spatial_ref_sys; drop table geometry_columns; create extension postgis;"

Postgis installation: type "geometry" does not exist

I am trying to create table with Postgis. I do it by this page. But when I import postgis.sql file, I get a lot of errors:
ERROR: type "geometry" does not exist
Does anybody know how can I fix it?
I had the same problem, but it was fixed by running following code
CREATE EXTENSION postgis;
In detail,
open pgAdmin
select (click) your database
click "SQL" icon on the bar
run "CREATE EXTENSION postgis;" code
If the Postgis-Extension is loaded, then your SQL perhaps does not find the geometry-type because of missing search-path to the public schema.
Try
SET search_path = ..., public;
in the first line of your scsript. (replace ... with the other required search-paths)
You can do it from terminal:
psql mydatabasename -c "CREATE EXTENSION postgis";
To get psql to stop on the first error, use -v ON_ERROR_STOP=1 (which is off by default, which is why you see many errors). For example:
psql -U postgres -d postgis -v ON_ERROR_STOP=1 -f postgis.sql
The actual error is something like "could not load library X", which can vary on your situation. As a guess, try this command before installing the sql script:
ldconfig
(you might need to prefix with sudo depending on your system). This command updates the paths to all system libraries, such as GEOS.
This error may also occur if you try to use postgis types on another schema rather than public.
If you are creating you own schema, using postgis 2.3 or higher and encounter this error, do the following as stated here:
CREATE SCHEMA IF NOT EXISTS my_schema;
CREATE extension postgis;
UPDATE pg_extension
SET extrelocatable = TRUE
WHERE extname = 'postgis';
ALTER EXTENSION postgis
SET SCHEMA my_schema;
ALTER EXTENSION postgis
UPDATE TO "2.5.2next";
ALTER EXTENSION postgis
UPDATE TO "2.5.2";
SET search_path TO my_schema;
Then you can proceed to use postgis functinalities.
You must enable the extension on your database.
psql my_database -c "CREATE EXTENSION postgis;"
You also need to ensure that the user you are trying to use the postgis extension as, has access to the schema where postgis is setup (which in the tutorials I read is called 'postgis').
I just had this error, and it was solved because I had only given a new user access to the database. In the database I'd created, I ran:
grant all on schema postgis to USERNAME;
And this solved this error
run this query first:
"CREATE EXTENSION postgis"
The answers here may solve your problem, however if you already have postgis enabled on your DB, the issue may be that you are trying to restore a postgis table (with a geometry column) into a schema other than where your postgis extension is enabled. In pgAdmin you can click on the postgis extension and see which schema is specified. If you are trying to restore a table with geometry column into a different schema, you might get this error.
I resolved this by altering my postgis extension - however I'm not sure if that was necessarily the best way to do it. All I know is that it allowed me to restore the table.
First make sure you have (matching to pg version: psql -V) postgis installed:
sudo apt install postgis postgresql-9.6-postgis-2.3
Just before tables creation add:
db.engine.execute('create extension postgis')
db.create_all()
This has already been answered but I wanted to add a more thorough answer that explains why certain commands work, and in what circumstances to use them, and of course, how to figure out which circumstances you are in.
First, you need to check that PostGIS is actually installed on your box. When connected to postgres, such as via psql, run:
SELECT PostGIS_Full_Version();
If it's not installed, look up distro- and version-specific instructions for installing PostGIS and install it.
Assuming PostGIS is installed, the error is usually the result of not having "created" (this is an unfortunately misleading use of language, the effect is more like "enabling" the extension) the extension for the particular database. The way PostgreSQL is set up, by default new databases do not come with any extensions enabled, and you need to enable ("create") them per-database. In order to do this you need to run the following command.
It only needs to be run once:
CREATE EXTENSION postgis;
I think you need superuser privileges for the particular database in question, in order to run this command.
Assuming postgres is configured so that the permissions allow, you can execute this command from the command line by running the following command:
psql my_database -c "CREATE EXTENSION postgis;"
You may need to use the -U flag and specify a user.
In some cases, however, the extension may have already been created, and installed under a different schema than public, and the problem may be one of permissions. This can arise like in the situation #mozboz describes, if you create a new user but don't give it access to the schema. To detect this case, look for a separate schema in the database, with a table called spatial_ref_sys, as this is created when the extension is created.
In this case you may need to run, when connected to the database:
GRANT USAGE ON SCHEMA schema_name TO username;
In my experience, this situation is rare, and I have never found any reason to set things up this way. The schema_name is often, but not always postgis. By default if you run the first command here, it will create the extension under the public schema.
I think USAGE is usually sufficient for most cases, but you might want to grant more privileges if you want the user to be able to actually edit data; the only time this has ever come up for me was adding new projections to spatial_ref_system, but even this is rare as by default that table includes most commonly used projections.
Or...
cursor.execute('create extension postgis')
in your python program, using a current cursor from psycopg2.
My experience was I was trying to load the ism file to a different file than "public". I initialised the postgis extension on another schema other than public. So that didn't work. So finally I had to remove the extension, and than created the postgis extension in public. After that I could load the osm into my new schema
Osm always looks for the extension file in public, irregardless of where u intend to install the osm files in another schema
Verify the public search_path is not included for the user:
SELECT usename, useconfig from pg_user;
-- { search_path=pg_catalog, public }
SHOW SEARCH_PATH;
-- public, topology
Method #1: SET the search_path to public
SET search_path = "public";
CREATE TABLE IF NOT EXISTS sample_geom
(
geom_1 GEOMETRY,
geom_2 GEOMETRY(Polygon, 4326) NOT NULL
);
ALTER TABLE sample_geom
OWNER TO root;
Method #2: Use the qualified object name for the GEOMETRY object type (public.GEOMETRY)
CREATE TABLE IF NOT EXISTS sample_geom
(
geom_1 public.GEOMETRY,
geom_2 public.GEOMETRY(Polygon, 4326) NOT NULL
);
ALTER TABLE sample_geom
OWNER TO root;
Source: Demystifying Schemas & search_path through Examples
Using pgAdmin 4,you can sort this:
Click on the SQL query button (or go to Tools >
Query Tool).
Enter the following query into the query text field to load the PostGIS spatial extension and Click the Play button in the toolbar (or press F5) to “Execute the query.”
CREATE EXTENSION postgis;
Succesful feedback
Now confirm that PostGIS is installed by running a PostGIS function:
SELECT postgis_full_version();
Code
Feedback