What are int8, int64 and string in PostgreSQL - postgresql

At the interview, they were given the task to make tables with int8, int64 and string types but in PostgreSQL no such types. What are int8, int64 and string in PostgreSQL?
I fount that int64 may be BIGSERIAL or BIGINT and String is Varchar

They were trying to fool you by showing you data types that don't exist, with the exception of int8, which is the internal name for the standard bigint type). You would use text for strings. int64 doesn't exist in PostgreSQL.

Related

Convert BigInt to Macaddr in Postgress

I have a client table that has IP and macaddr saved as BIGINT.
I was able to convert IP to text with ::inet, how do I transform BIGINT to macaddr?
Example of MACaddr as saved: 8796349528980
You can convert your BIGINT to HEX, and abuse the macddr type of postgresql to format that, like that:
SELECT TO_HEX(8796349528980)::macaddr;

How to use uuid with postgresql gist in EXCLUDE constraint

I'm getting error while I use Exclude constraint using gist
ALTER TABLE tbl_product ADD EXCLUDE USING gist (base_id WITH =, lifetime WITH &&);
ERROR: data type uuid has no default operator class for access method "gist"
HINT: You must specify an operator class for the index or define a default operator class for the data type.
Note:
base_id datatype is uuid,
lifetime datatype is period
I am using PostgreSQL 9.4. I have to use 9.4 only as I don't have any other option since I am unable to install temporal extension in 9.5, 9.6 and 10 are gives an error.
You'll need the btree_gist extension for that:
btree_gist provides GiST index operator classes that implement B-tree equivalent behavior for the data types int2, int4, int8, float4, float8, numeric, timestamp with time zone, timestamp without time zone, time with time zone, time without time zone, date, interval, oid, money, char, varchar, text, bytea, bit, varbit, macaddr, macaddr8, inet, cidr, uuid, and all enum types.
Unfortunately support for uuid was only added in v10.
With v10, you should be able to use
base_id gist_uuid_ops WITH =
in your exclusion constraint.
With 9.4, you could cast the column to a different type first:
(base_id::text) gist_text_ops WITH =
The accepted answer is correct, btree_gist is needed, however the suggested solution does not work (at least not in v12 in 2021). If you've been getting errors like in original question you should do the following:
CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE tbl_product ADD EXCLUDE USING gist (base_id WITH =, lifetime WITH &&);
As a bonus, I've been using it in Elixir & Ecto 3.5 and here's how to do this in Ecto migration:
execute "CREATE EXTENSION IF NOT EXISTS btree_gist"
create constraint(:tbl_product, "constraint_name", exclude: ~s|gist ("base_id" WITH =, lifetime WITH &&)|)

setting the length of an integer column in Postgres

I have a table called "User_details" in my Postgres database with a column as "Mobile_no" with data type as Integer.
Now when I try inserting an integer value of length 10, I get the following error
ERROR: integer out of range
I know this can be resolved if i set the data type to bigint or may be even numeric(10,0) but i want to use Int as the data type.
Any possible solution to set the length of an Integer column in Postgres?
On PostgreSQL an integer type can hold values between -2147483648 and +2147483647 so if the value to insert is "greater than" there is no way to store it on database, only by modifying the data type for example to bigint you will be able to store greater values (bigint can hold values between -9223372036854775808 to +9223372036854775807).
Please check https://www.postgresql.org/docs/current/static/datatype-numeric.html

postgresql: btree_gist does not exist

I need to use btree_gist in order to index three fields: a postgis geometry, a date and bigint.
I have postgresql-contrib installed, and I'm able to create the extension without any problem using: CREATE EXTENSION btree_gist; the extension is visible in the extensions list in pgAdmin, but it is not installed according to the command \dx in psql.
Obviously trying to create an index with btree_gist results in an error stating that btree_gist does not exist.
I'm running pqsl 9.5.4.
Is there something that I'm missing?
According to manuels
https://www.postgresql.org/docs/9.5/static/btree-gist.html
btree_gist provides GiST index operator classes that implement B-tree equivalent behavior for the data types int2, int4, int8, float4, float8, numeric, timestamp with time zone, timestamp without time zone, time with time zone, time without time zone, date, interval, oid, money, char, varchar, text, bytea, bit, varbit, macaddr, inet, and cidr.
PostGIS geometries not included here and also Postgis geometries has own index type GIST
And there is a similar question here
Postgres GIST vs Btree index

How to convert primary key from integer to serial?

In a Postgres 9.3 table I have an integer as primary key with automatic sequence to increment, but I have reached the maximum for integer. How to convert it from integer to serial?
I tried:
ALTER TABLE my_table ALTER COLUMN id SET DATA TYPE bigint;
But the same does not work with the data type serial instead of bigint. Seems like I cannot convert to serial?
serial is a pseudo data type, not an actual data type. It's an integer underneath with some additional DDL commands executed automatically:
Create a SEQUENCE (with matching name by default).
Set the column NOT NULL and the default to draw from that sequence.
Make the column "own" the sequence.
Details:
Safely rename tables using serial primary key columns
A bigserial is the same, built around a bigint column. You want bigint, but you already achieved that. To transform an existing serial column into a bigserial (or smallserial), all you need to do is ALTER the data type of the column. Sequences are generally based on bigint, so the same sequence can be used for any integer type.
To "change" a bigint into a bigserial or an integer into a serial, you just have to do the rest by hand:
Creating a PostgreSQL sequence to a field (which is not the ID of the record)
The actual data type is still integer / bigint. Some clients like pgAdmin will display the data type serial in the reverse engineered CREATE TABLE script, if all criteria for a serial are met.