Insert script to auto populate numbers 1-1000? [duplicate] - postgresql

This question already has an answer here:
Populate Postgres database with numbers 1-1000?
(1 answer)
Closed 5 years ago.
I have a single table with a single column called id as shown below. I need to populate the table with numbers 1-1000, how can I do that with the scripting?

Use generate_series to this:
--Without column declaration
INSERT INTO "MyTable" SELECT generate_series(1, 1000);
--With column declaration
INSERT INTO "MyTable"(id) SELECT generate_series(1, 1000);

Related

Postgres 11 INSERT ON CONFLICT SELECT * [duplicate]

This question already has an answer here:
Postgres 9.5 ON CONFLICT DO SELECT
(1 answer)
Closed 3 years ago.
I am trying to insert a row into a table, if that insert succeeds I would to return the newly inserted row. If that insert fails, I would to select a row using some of the values I already know to be on that row.
For example, table 'reports' has 3 columns, so far I have the following:
INSERT INTO reports (col2, col3) VALUES ($1, $2)
ON CONFLICT ON CONSTRAINT custom_index DO
SELECT * FROM reports WHERE col1=$1 AND col2=$2;
For those who will ask, custom_index was created using:
CREATE UNIQUE INDEX custom_index ON reports (col2) INCLUDE (col3);
The current error I am getting is
syntax error at or near "SELECT"
Select is not allowed in the conflict.
Check the documentation https://www.postgresql.org/docs/current/sql-insert.html.
Related question is already answered.. Postgres 9.5 ON CONFLICT DO SELECT

Inserting values into a table not working [duplicate]

This question already has answers here:
INSERT COMMAND :: ERROR: column "value" does not exist
(2 answers)
Error "value" does not exist - postgresql INSERT INTO issue
(1 answer)
Why does Postgres say column does not exist? [duplicate]
(1 answer)
PostgreSQL - insert statement error, thinks value to insert is a column name
(1 answer)
Closed 3 years ago.
I am trying to insert a value(row) into my table users but it's giving error that column doesn't exist.
User table:
users
insert into users (id,name,gender,city,age,company)
values (1,omkar,"male","cuttack",24,"tcs");
I am getting column does not exists. And with some specific values I am getting hint don't know why.
Error screenshot
You must use single quotes for literal string values. Double quoting is interpreted as being an identifier, in this case a column name, which causes the error.
insert into users (id,name,gender,city,age,company)
values (1,'omkar','male','cuttack',24,'tcs');
If you set "Id" is auto increase, you can try bellow
insert into users (name,gender,city,age,company) values ('omkar','male','cuttack',24,'tcs');

Insert or select and returning the key [duplicate]

This question already has answers here:
Get id from INSERT or SELECT
(3 answers)
Return rows from INSERT with ON CONFLICT without needing to update
(1 answer)
Is SELECT or INSERT in a function prone to race conditions?
(3 answers)
Closed 4 years ago.
What is the easiest way to insert a row in a database and returning its id if that row doesn't exist, otherwise return the ID of that word?
INSERT INTO mytable (name)
SELECT 'd'
WHERE NOT EXISTS (SELECT id FROM mytable WHERE name='d')
RETURNING id
This code works only if the row doesn't exist.
I either do that and then a SELECT, or try the INSERT with ON CONFLICT DO NOTHING and then a SELECT again.
Is there a better solution?

POSTGRESQL: How to set the last id of a tables as start value of a sequence [duplicate]

This question already has answers here:
How to reset Postgres' primary key sequence when it falls out of sync?
(33 answers)
PostgreSQL increase a table's sequence with one query
(3 answers)
Can you create a sequence on a column that already exists in Postgres
(1 answer)
Postgres manually alter sequence
(7 answers)
Closed 4 years ago.
For inserting values into a table, I need to read the last primary key value of that table and start my sequence from there. But I get an error.
My query is as below:
CREATE SEQUENCE serial START(
SELECT cd.id + 1
FROM cd
ORDER BY cd.id DESC
LIMIT 1);
INSERT INTO cd(id, class)
SELECT (nextval('serial'), (
SELECT class_name
FROM another_table
WHERE some_condition
)
FROM cr
DROP SEQUENCE IF EXISTS serial;
And the error is as below:
ERROR: syntax error at or near "("
LINE 1: CREATE SEQUENCE serial START( SELECT cd.id + 1 FROM cd
How can I get the last value of the primary key and start my sequence from there?
I'm not allowed to alter table design, so I can not define a sequence for the primary key of the table.
Well, I solved my problem:
BEGIN TRANSACTION;
CREATE SEQUENCE serial START 1;
INSERT INTO cd(id, class)
SELECT (nextval('serial') + (select max(id)+1 from cd), (
SELECT class_name
FROM another_table
WHERE some_condition
)
FROM cr
DROP SEQUENCE IF EXISTS serial;
COMMIT;

How to search for a variable name in PostgreSQL? [duplicate]

This question already has answers here:
How to find a table having a specific column in postgresql
(6 answers)
Closed 5 years ago.
I want to search for a particular variable name in ‘PostgreSQL’ database. Similar to the following ‘Teradata’ query
Select TableName, ColumnName from
DBC.Columns
Where ColumnName like (‘%profile%’)
Is there a similar query in PostgreSQL?
Postgres documentation
SELECT table_name,column_name
FROM information_schema.columns
WHERE column_name like '%profile%'