Can not install hstore extension to new created schema - postgresql

I have an ubuntu 18.04 with postgres 9.5 installed.
My db "mydb" has the hstore installed. When I do "\dx store", I do have
List of installed extensions
Name | Version | Schema | Description
--------+---------+--------+--------------------------------------------------
hstore | 1.3 | public | data type for storing sets of (key, value) pairs
(1 row)
When I do a pg_restore with a certain backup file, a new schema also called "mydb" is created, but it does not contain the "hstore" extension. The result of the "\dx" command is the same. hstore is in my template1 already.
The pg_restore fails with
pg_restore: [archiver (db)] could not execute query: ERROR: type "hstore" does not exist
Can anyone point out where the problem is?
Thanks

Was the backup created with the same version of postgresql?
The dump / restore cycle generally assumes that the database was created in advance, but the dump should create the extensions.
Can you search the backup to see if it contains a directive to create the extension for your schema:
CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA public;
you will need to use the pg_restore command to stdout e.g.
pg_restore mydump.dump|more
You may also need to check that your schema is created before the extension, if you are not using the public schema.
Can you try the following to create the hstore extension in your mydb schema before the restore:
CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA mydb;
Alternatively, the hstore extension may not be installed properly.
# locate hstore|grep postgresql
# dpkg -S /path/to/hstore.so

Related

pg_restore: error: could not execute query: ERROR: extension "pg_stat_statements" must be installed in schema "heroku_ext"

Due to this change by heroku, I am not able to restore my Heroku Postgres backup.
Following errors are thrown while restoring:
pg_restore: error: could not execute query: ERROR: extension "pg_stat_statements" must be installed in schema "heroku_ext"
Command was: CREATE EXTENSION IF NOT EXISTS "pg_stat_statements" WITH SCHEMA "public";
pg_restore: error: could not execute query: ERROR: extension "pg_stat_statements" does not exist
Command was: COMMENT ON EXTENSION "pg_stat_statements" IS 'track planning and execution statistics of all SQL statements executed';
Command was: CREATE EXTENSION IF NOT EXISTS "postgis" WITH SCHEMA "public";
Any many such errors.
I contacted Heroku support, and they have provided the following step to try fix the issue manually.
Here are the steps in detail:
Download a backup of your database.
Convert the dump file to a .sql file with: pg_restore -f
Modify the CREATE EXTENSION commands to use CREATE EXTENSION IF NOT EXISTS
extension_name WITH SCHEMA heroku_ext. You can do this by using sed or a text editor
of your choice.
Restore the backup using pg_restore to Heroku Postgres from your app. The extensions
in your production database will now be owned by the correct schema; future
migrations, backups, and restores should proceed without needing to repeat this
process.
I have modified the .sql file as per step3, but I am not able to figure hot how to convert this .sql file to a .dump file to be restored on the Heroku app.
I have tried heroku pg:psql --app <app> <sql_file> but it does not work and throws errors.
Can someone help me in importing this .sql file to Heroku?
Steps followed to solve the issue:
Download the DB dump file from Heroku.
convert the dump to a .sql file using pg_restore.
Reset the Heroku DB using pg:reset
log in to the DB using pg:psql and install the extensions in the "heroku_ext" schema
Now execute the .sql file on the DB using pg:psql and check the errors
modify the .sql file lines that are causing the errors. Most of the errors can be solved by replacing "public" with "heroku_ext" at that lines.
After modifying the sql, again follow steps 3 to step 7 until all errors are fixed.
How to restore a Heroku database dump in a non-Heroku environment
Download the latest db dump from heroku
heroku pg:backups:download --app <app>
Drop local db if exists and create a new one
psql <db_url> -c "DROP DATABASE IF EXISTS <db_name>"
psql <db_url> -c "CREATE DATABASE <db_name> WITH ENCODING 'UTF8' TEMPLATE template0"
Here comes the important part
Create the heroku_ext schema inside the newly created database
psql <db_url><db_name> -c "CREATE SCHEMA IF NOT EXISTS heroku_ext AUTHORIZATION <db_user>"
Create the extention in the heroku_ext schema
psql <db_url><db_name> -c "CREATE extension IF NOT EXISTS <extension> WITH schema heroku_ext"
Grant the necessary permissions, eg:
psql <db_url><db_name> -c "GRANT ALL ON SCHEMA heroku_ext TO public"
psql <db_url><db_name> -c "GRANT USAGE ON SCHEMA heroku_ext TO public"
Add schema to search_path
psql <db_url><db_name> -c "SET search_path TO heroku_ext,public"
Grant permissions to the schema to the relevant user
psql <db_url><db_name> -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA heroku_ext TO <db_user>"
Finally...
Restore the database dump
pg_restore --no-acl --no-owner -h <db_host> -U <db_user> -d <db_name> ./latest.dump
I had the same issue and found the answer in this question.
https://stackoverflow.com/a/73773443/17517157
Just add --extensions 'postgis' to your restore command.
You can ignore the error and manually create the extension in the required schema. If you want to avoid the error message, drop the extension in the database before dumping it. That should not be a problem, because nothing else in your database should depend on that extension.

Error when restoring PostGIS database: "type geometry does not exist"

So a colleague of mine is leaving our company and he used pg_dump (indirectly, through pgAdmin III) to dump his local database. He put the .backup file on the company server, and I'm now using pg_restor (indirectly, through pgAdmin III) to restore his tables into the company database.
But I noticed that only the tables without a geometry were restored.
When looking closely at the logs, I noticed that the error was the following (sorry about the French locale):
pg_restore: [programme d'archivage (db)] could not execute query: ERREUR: le type « geometry » n'existe pas
LIGNE 3 : geom geometry(Point,5699),
^
La commande était : CREATE TABLE cbt_poste (
id integer NOT NULL,
geom geometry(Point,5699),
nom_du_pos character varying(32),
d...
Basically, it states that the type Geometry does not exist. But when I create a table with a Geometry column, in whatever schema of the database, it works successfully.
So my problem is essentially:
When I create a table with a Geometry column, everything works...
But when pg_restore creates a table with a Geometry column, everything happens as if the PostGIS extension never had existed.
Any experience of this kind of issue?
I may assume, there's no PostGIS extension installed at the database, where you're trying to restore the dump. That's why spatial data types (geometry, in particular) are not getting recognized. You may try to install PostGIS and retry pg_restore.
See if you have postgis installed writing this in psql:
SELECT PostGIS_full_version();
if not installed, you can install it following the instructions in postgis install instructions
If you have postgis installed, you need to make sure the extension is available in your database, if it is not, you can create it with:
CREATE EXTENSION postgis;

pg_restore WARNING: errors ignored on restore: 62

I was given a database file , i don't know the userid who dumped it or it's privileges .
I use postgresql 9.6.7-1 with pg_admin4(v3.0) , OS: windows 10
First, i created a database in pgadmin with same name as the given file.
I used the restore option to restore the file but after some seconds
i got type of messages like :
pg_restore: executing SEQUENCE SET xxxx
pg_restore: [archiver (db)] Error from TOC entry 4309; 0 0 SEQUENCE SET xxxx postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.xxxx" does not exist
LINE 1: SELECT pg_catalog.setval('public.xxxx', 1, false);
^
Command was: SELECT pg_catalog.setval('public.xxxx', 1, false);
and below all, the warning :
"WARNING: errors ignored on restore: 62"
comparing with other's answers i dont even get a single bit of data restored.
I have tried also with
pg_restore
command but i get the same result .
It looks like the dump you were given is not a complete backup. It only has the data, not the object definitions. That is, it was created by pg_dump using -a, --data-only, or --section=data.
Unless you already know what the object definitions are from some other source (e.g., an existing database server with the same schema definitions, or a dump file generated with pg_dump -s), you will have a hard time loading this data.
{solved}
please check the version of the Postgresql pgAdmin4 and the version of ".Sql" file you want to import into your database ,and instead of restoring at public you should directly restore at the database itself.
2:- drop the database and again re-create the fresh database(name='xyz') & at the same database ('xyz'** ->RightClick->Restore->Filename->'select(formate ".sql")->Restore**) than->refresh the database.
and by doing that it'll create i new Schema just above public Schema..

Installing extension postgresql 9.4 for all schemes

How to install an extension pg_stat_statements once for all schemes in a single database postgresql 9.4?
CREATE EXTENSIONS pg_stat_statements;
installs only in the public schema
UPD:
I solved the problem as follows:
sudo -u postgres psql -d "template1" -c "CREATE EXTENSION pg_stat_statements;"
sudo -u postgres psql -d "template1" -c "ALTER EXTENSION pg_stat_statements SET SCHEMA pg_catalog;"
The newly created database will have an extension in pg_catalog scheme that will allow this extension to work with all other schemes in the database.
As given here, although the Extension installs in Public by default, you can change and install it on any Schema using the given syntax.
From your question it seems that you need to install the extension in all Schemas.... Please note that you don't need to install a given Extension in all Schemas (within a Database). The Schema is just a placeholder for all the DB objects, but once installed, the Extension as such, is available to all Schemas in the Database (and needn't be installed for each Schema).

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.