Troubleshooting ERROR: extra data after last expected column - postgresql

I'm trying to add CSV data to my Postgres table:
CREATE TABLE IF NOT EXISTS Review (
review_id BIGSERIAL NOT NULL,
product_id_Metadata INTEGER,
date DATE,
summary VARCHAR(255),
body VARCHAR(500),
rating INTEGER,
recommend BOOLEAN,
reported BOOLEAN
reviewer_name VARCHAR(50),
reviewer_email VARCHAR(50),
response VARCHAR(255),
helpfulness INTEGER,
FOREIGN KEY product_id(product_id) REFERENCES Metadata(product_id)
);
Running the CMD:
COPY review (product_id_Metadata, summary, body, rating, recommend, reported, reviewer_name, reviewer_email, response, helpfulness) FROM '/Users/luke82601/Desktop/csv/reviews.csv' DELIMITER ',' CSV HEADER;
The CSV file has the same number of columns as my Postgres table, so I'm not sure why this error occurs

Related

Not able to create a foreign key; Relation " " does not exist

I am trying to create a foreign key called 'user_id' for a 'transactions' table where the user_id references the 'user_accounts' table 'id' column. I keep getting an error when I execute the script that says:
SQL Error [42P01]: ERROR: relation "user_accounts" does not exist
The table clearly exists as I have been populating the user_accounts table with data that can be viewed in dbeaver. I am using Postgres and I am aware that quotes/capitalization can really make things difficult but I have executed my entire script without capitalizing or using quotes on any of the table or column names. Although, I did capitalize some of my column data types and I am wondering if that is the issue here? If so, what direction should I take to get my foreign key to work?
My script:
create table if not exists user_accounts (
id serial primary key,
first_name VARCHAR(30),
last_name VARCHAR(30),
username VARCHAR(20),
password VARCHAR(20),
deposit INT,
creditScore INT
)
create table if not exists transactions (
transaction_id serial primary key,
user_id INT references user_accounts(id) not null,
transaction_type VARCHAR(20),
amount INT
)

IBM Db2 on Cloud script creating tables in the wrong schema

On IBM Db2 on Cloud I have imported a script. I created a new schema under which I want to have the new tables created, but when I run the script, it keeps trying to create the tables in a previous schema. Not sure how to get the scripts to create the tables in the new schema.
I have tried the below script without the .SQL_GROUPING_SORTING and it tries to add the tables to a different schema. I have changed the default schema in the Run SQL window within db2 to SQL_GROUPING_SORTING and am now getting the error
""KZF72118" does not have the privilege to perform operation "IMPLICIT CREATE SCHEMA".. SQLCODE=-552, SQLSTATE=42502, DRIVER=4.26.14"
DDL statement for table 'HR' database:
CREATE TABLE EMPLOYEES.SQL_GROUPING_SORTING (
EMP_ID CHAR(9) NOT NULL,
F_NAME VARCHAR(15) NOT NULL,
L_NAME VARCHAR(15) NOT NULL,
SSN CHAR(9),
B_DATE DATE,
SEX CHAR,
ADDRESS VARCHAR(30),
JOB_ID CHAR(9),
SALARY DECIMAL(10,2),
MANAGER_ID CHAR(9),
DEP_ID CHAR(9) NOT NULL,
PRIMARY KEY (EMP_ID));
CREATE TABLE JOB_HISTORY.SQL_GROUPING_SORTING (
EMPL_ID CHAR(9) NOT NULL,
START_DATE DATE,
JOBS_ID CHAR(9) NOT NULL,
DEPT_ID CHAR(9),
PRIMARY KEY (EMPL_ID,JOBS_ID));
CREATE TABLE JOBS.SQL_GROUPING_SORTING (
JOB_IDENT CHAR(9) NOT NULL,
JOB_TITLE VARCHAR(15) ,
MIN_SALARY DECIMAL(10,2),
MAX_SALARY DECIMAL(10,2),
PRIMARY KEY (JOB_IDENT));
CREATE TABLE DEPARTMENTS.SQL_GROUPING_SORTING (
DEPT_ID_DEP CHAR(9) NOT NULL,
DEP_NAME VARCHAR(15) ,
MANAGER_ID CHAR(9),
LOC_ID CHAR(9),
PRIMARY KEY (DEPT_ID_DEP));
CREATE TABLE LOCATIONS.SQL_GROUPING_SORTING (
LOCT_ID CHAR(9) NOT NULL,
DEP_ID_LOC CHAR(9) NOT NULL,
PRIMARY KEY (LOCT_ID,DEP_ID_LOC));
With the Db2 on Cloud Lite Plan
The Lite plan uses one database schema.
So the only schema you can use is the one that matches your user name. In your case this would be KZF72118
Create your tables with out a schema name, and they will be created in schema KZF72118.
You would need to use one of the other plans to remove this restriction

PostgreSQL displaying integer inserted without commas, with commas (formatted like currency or similar)

SQL Server guy, new to PostgreSQL. Created the below table, performed the below insert, then ran a SELECT to see the data, yet the row shows the integer formatted with columns to break up the integer. Is this just a formatting style in the HeidiSQL utility I'm using, or is the data actually being stored as x,xxx,xxx,xxx rather than xxxxxxxxxx.
Table:
CREATE TABLE customer (
business_partner_id INTEGER PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
organisation_name VARCHAR(200),
date_of_bith DATE,
gender VARCHAR(50),
customer_type VARCHAR(50),
store_joined VARCHAR(10),
store_joined_date DATE,
created_date_time TIMESTAMP,
updated_date_time TIMESTAMP);
Insert:
-- Insert a customer
INSERT INTO customer VALUES
(1029884766,'James','Matson','Unknown','1980-02-17','M','Standard','303',CURRENT_DATE,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)
Query results:

Why is the format of the sql files affecting if they can run or not in PG?

I have placed a file in my docker-entrypoint-initdb.d/ directory. Here is what is in the file:
CREATE TABLE user_test (
user_id INTEGER,
name VARCHAR(100),
email VARCHAR(128),
active_flg BOOLEAN,
type VARCHAR(20),
CONSTRAINT pk_user PRIMARY KEY (user_id)
);
The error I am getting is psql:/docker-entrypoint-initdb.d/0001-initial-database-design.sql:8: ERROR: syntax error at or near "CREATE".
What am I missing in being able to run a file? How do I change this file to work?
USER is a reserved keyword in Postgres, see the documentation. In general, you should avoid naming your tables and columns using reserved SQL keywords. If you really wanted to proceed as is, then place user into double quotes:
CREATE TABLE "user" (
user_id INTEGER,
name VARCHAR(100),
email VARCHAR(128),
active_flg BOOLEAN,
type VARCHAR(20),
CONSTRAINT pk_user PRIMARY KEY (user_id)
);
But, keep in mind that if you choose to name your table user, then you will forever have to escape it with double quotes.

POSTgreSQL 9.5: invalid input syntax for integer when trying to copy from CSV

I've successfully created the following table:
CREATE TABLE tampadocs (
fname varchar(255),
lname varchar(255),
pracName varchar(255),
address varchar(255),
city varchar(255),
state varchar(255),
zip varchar(255),
spec varchar(255),
phone varchar(255),
totalMD integer,
avgPt integer,
mdName varchar(255),
notes varchar(255));
Then run the following to import data from a CSV to the table:
COPY tampadocs
FROM 'C:\Users\bam\Desktop\tampadocs.csv' DELIMITERS ',' CSV;
I receive the following error:
ERROR: invalid input syntax for integer: "Total MDs"
CONTEXT: COPY tampadocs, line 1, column totalmd: "Total MDs"
I've looked at each value in the Total MDs column but they are just numbers, so I'm not sure what I'm missing. Any help would be greatly appreciated!
Sounds like your file 'tampadocs.csv' has a header line. Can you check on that? The copy syntax you're using is assuming there's only data in the file.
If there's a header line in your file, you can try the following:
COPY tampadocs
FROM 'C:\Users\bam\Desktop\tampadocs.csv'
WITH (FORMAT CSV, DELIMITER ',', HEADER);
That lets the copy statement know to expect a header line in the file. The full syntax for COPY is available here.