Do I have to create extension on every connect to database? - postgresql

I want to use uuid_generate_v4 in PostgreSQL. I am currently using version 11, but plan to upgrade to 12.
When I try to call the function I get this error after every connect:
test=# SELECT uuid_generate_V4();
ERROR: function uuid_generate_v4() does not exist
LINE 1: SELECT uuid_generate_V4();
And I have to rerun:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
Minimum "working" example
CREATE DATABASE test;
\connect test;
SELECT uuid_generate_V4(); -- Fails makes sense
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_V4(); -- Works
\connect postgres --- Close connection to test
\connect test;
SELECT uuid_generate_V4(); -- Fails makes no sense
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_V4(); -- Works
Do I really need to add/create the extension at every connect or am I doing something wrong?
I have the same problem with other extensions as well, but uuid is the most crucial to get working.

Related

Postgres GIN Index Not Creating Through Liquibase Scripts

I am trying to create a GIN index through liquibase scripts.
CREATE INDEX IF NOT EXISTS index_name ON schema_name.table_name USING gin (column1, lower(column2) COLLATE pg_catalog."default" gin_trgm_ops)
Each time migration for the liquibae changeset fails with the following exception:
liquibase.exception.DatabaseException: ERROR: operator class "gin_trgm_ops" does not exist for access method "gin"
The problem is that this query works when run directly from pgAdmin query tool. There seems to be some issue when running this query from liquibase scripts.
I had to create the following extensions before creating the index from pgAdmin query tool. I am creating these extensions in the liquibase script too.
CREATE EXTENSION IF NOT EXISTS pg_trgm with schema public;
CREATE EXTENSION IF NOT EXISTS btree_gin with schema public;
Following is the part of my liquibase script that is creating the extensions and indexes. Btree index creating queries work well with the liquibase scripts. It fails with the above exception when trying to create GIN index.
CREATE EXTENSION IF NOT EXISTS pg_trgm with schema public;
CREATE EXTENSION IF NOT EXISTS btree_gin with schema public;
The following works:
CREATE INDEX IF NOT EXISTS index_name ON table_name USING btree (col1,col2 COLLATE pg_catalog."default")
CREATE INDEX IF NOT EXISTS index_name ON table_name USING btree (col1, col2 COLLATE pg_catalog."default")
But this fails:
CREATE INDEX IF NOT EXISTS index_name ON table_name USING gin (col1, lower(col2) COLLATE pg_catalog."default" gin_trgm_ops)
Wondering if liquibase is running the script, is not running with the public schema in the search path, resulting in an inability to find the gin_trgm_ops operator class you're using to create the index. The error message would certainly indicate this.
Either add it to the search path explicitly and try again (it should work), or create the extension in the pg_catalog schema so that its available implicitly. See here: https://www.postgresql.org/docs/9.1/ddl-schemas.html (section 5.7.5).
You can do this by running the following in place of your existing pg_trgm create extension command.
CREATE EXTENSION IF NOT EXISTS pg_trgm with schema pg_catalog;
This followed by your other commands should do the trick.
Following worked for me instead of using createIndex statement in changeSet I used custom sql statement like
<changeSet author="me" id="26">
<sql>CREATE INDEX my_table_hours_idx ON my_table USING GIN(hours)</sql>
</changeSet>

PostgreSQL 9.5: Hide password from dblink connection

I want to you table which is located in the other database.
I am using the dblink for this.
Procedure:
Step 1: Created extension.
CREATE EXTENSION dblink;
Step 2: Making dblink connection string.
select dblink_connect('con','host=127.0.0.1 dbname=makdb user=postgres password=postgres');
Step 3:
select * from dblink('con','select cola,colb from tbl_test') as tbl(cola int,colb varchar(10));
My Question: How do i hide password in step 2?
By searching i came to know that i need to create .pgpass file. But got stuck in how to create and in which step i need to use that file name.
Install dblink extension:
CREATE EXTENSION dblink;
Install postgres_fdw extension (which can be used to access data stored in external PostgreSQL servers):
CREATE EXTENSION postgres_fdw;
Create a new foreign server connection:
CREATE server myserver foreign data wrapper postgres_fdw
OPTIONS (dbname 'foreign_dbname', host 'foreign_host');
Create a user mapping for the foreign server connection that you recently created and your database.
CREATE USER MAPPING FOR "user_in_current_database"
SERVER myserver OPTIONS (user 'foreign_user', password 'foreign_password');
Select some fields in a remote db with the conexion created. Notice that you does not need use the user and password anyrmore.
SELECT tmp_table.*
FROM dblink(
'myserver',
'
SELECT field1,
field2
FROM table
'
)
AS tmp_table(
field1 TEXT,
field2 BIGINT
);
More info:
https://www.postgresql.org/docs/9.5/postgres-fdw.html
https://www.postgresql.org/docs/current/sql-createserver.html
https://www.postgresql.org/docs/current/sql-createusermapping.html

PostgreSQL to Oracle - Can i change OWNER in Oracle?

I am trying to edit PostgreSQL schema script and make it executable in Oracle (Oracle Express). In PostgreSQL were under the each CREAT TABLE these commands:
ALTER TABLE table_name OWNER TO user;
For example table_name is appuser and user is projectX.
The table is successfully created, but there is an error: ORA-01735: invalid ALTER TABLE option
I have also created another user in my scheme (projectX), but the error is still there. So I am confused. Does this command ALTER TABLE table_name OWNER TO user; even exists in Oracle database?

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.

How does one drop a template database from PostgreSQL?

postgres=# DROP DATABASE template_postgis;
ERROR: cannot drop a template database
http://www.postgresql.org/docs/9.1/static/manage-ag-templatedbs.html makes it seem like if I set template_postgis.datistemplate = false, I'll be able to drop it, but I don't know how to set that.
postgres=# UPDATE pg_database SET datistemplate='false' WHERE datname='template_postgis';
UPDATE 1
postgres=# DROP DATABASE template_postgis;
DROP DATABASE
postgres=#
You can use the alter database command. Much simpler and safer than upsetting metadata.
postgres=# create database tempDB is_template true;
CREATE DATABASE
postgres=# drop database tempDB;
ERROR: cannot drop a template database
postgres=# alter database tempDB is_template false;
ALTER DATABASE
postgres=# drop database tempDB;
DROP DATABASE
postgres=#
Documentation
update pg_database set datistemplate=false where datname='template0';
update pg_database set datistemplate=false where datname='template1';
....
Creating script to analyze new cluster ok
Creating script to delete old cluster ok
Checking for hash indexes ok
Upgrade Complete
----------------