Importing csv in postgres from pgAdmin 4 | google-cloud-sql - postgresql

I created a database and a user from the dashboard. I used cloud-sql-proxy to connect to the database and was able to succesfully connect from pgAdmin4. Now I tried to use the functionalities of pgAdmin4 to upload a csv to a table which had already been created by an appengine app which I have. When I try to upload this csv I get the error: ERROR: permission denied for table data
The command pgAdmin4 is trying to run is: --command " "\\copy public.data (\"Date\", \"Open\", \"Close\", \"High\", \"Low\") FROM 'C:/Users/username/folder/datafile.CSV' DELIMITER ',' CSV HEADER ;""
I tried to log in from the cloud console and grant all permissions to this user but this also failed.
I found this article on how to upload a csv from a Cloud Storage https://cloud.google.com/sql/docs/postgres/import-export/import-export-csv but this is not exacly what I want to achieve here...

Related

Problem restoring databse between 2 RDS instances using pg_dump and pg_restore

I'm having difficulty restoring a DB to an AWS RDS Postgresql instance. Context is that i am backing up from one RDS instance and restoring to another RDS insurance. They both have the same version of Postgresql 9.6.5.
I was able to take a dump using the following command:
./pg_dump.exe -U dbuser -W -h prod-pgsql-rds.3ft5coqxjdnq.eu-west-2.rds.amazonaws.com -d devdb > c:\tmp\backup.sql
From the resulting .sql file, I then attempted a restore to another RDS instance which is also using Postgresql 9.6.5 using below command:
./pg_restore.exe -U dbuser -d testdevdb -h dev-pgsql-rds.cym8coqx52lq.eu-west-2.rds.amazonaws.com "c:\tmp\backup.sql"
*I also tried the -f switch in the above restore command instead of the " " quotes before/after the file name
But when I try to restore it to a newly created database I get the following error:
pg_restore: [archiver] input file does not appear to be a valid archive
Can anyone help? FYI, I am using PGAdmin 4 via Windows PowerShell. I have to edit some of the values in the strings above due to data sensitivity.
pg_restore is only used for the other, non-plain-text output formats that pg_dump can output. For .sql dumps, you just use psql. See the docs on restoring from backups.
In a Unix env, you'd do psql [yourflags] < /tmp/backup.sql, but I'm unfamiliar with powershell and don't know if it supports < for input redirection; hopefully either it's present or you know the equivalent PowerShell syntax.
So I couldn't get psql or pg_restore to work so opted to import the .SQL file into via the SQL query tool in PGAmdin. This through up some errors so had to make several changes to the .SQL file and perform below:
Commented out a couple of lines that were causing errors
Elevated permissions for the user and made him the owner of for the Schema and DB properties by right-clicking on these via PGAdmin
The .sql file was making several references to the user from the source RDS DB so had to do a find and replace with a user account created for the destination RDS DB. Alternatively, I could have just created a new user on the destination DB with the same username and password as the source DB and then make him the owner in ref to step 2.

Azure_superuser account password for Postgresql database on Azure

I have provisioned Azure Database for Postgresql, I'm trying to execute copy command in c# Azure function to load data from csv file to the database.
It throws an error saying ERROR: must be superuser to COPY to or from a file
The question is how do I use the user Azure_superuser, where do I get password for this account. This account is automatically created while setting up the PostgreSQL on Azure.
Because this is a PaaS service, you will not have access to the Super User account. You will have access to the Admin account:Azure PostgreSQL Server Admin Document

Syntax error importing data from S3 into Amazon RDS using SQL

I have a 5GB CSV file stored in S3. I have a PostgreSQL database on Amazon RDS set to only allow access from my IP address. I'm accessing it from my Postgres client (Postico).
I'm trying to upload data with the instructions on the AWS site (https://docs.aws.amazon.com/redshift/latest/dg/t_loading-tables-from-s3.html)
I have a user with unlimited access to S3 and RDS. Specifically, they have the AmazonS3FullAccess and AmazonRDSFullAccess policies.
I'm using this code:
COPY schema.table FROM 's3://(filled_in_with_bucket_name)/file.csv'
access_key_id 'aws_access_key_id=(filled_in_with_access_key)'
secret_access_key 'aws_secret_access_key=(filled_in_with_secret_key)'
CSV;
I get this error ERROR: syntax error at or near "access_key_id"
I have also tried variants with
COPY schema.table FROM 's3://(filled_in_with_bucket_name)/file.csv'
WITH CREDENTIALS 'aws_access_key_id=(filled_in_with_access_key);aws_secret_access_key=(filled_in_with_secret_key)'
CSV;'
COPY schema.table FROM 's3://(filled_in_with_bucket_name)/file.csv'
CREDENTIALS 'aws_access_key_id=(filled_in_with_access_key);aws_secret_access_key=(filled_in_with_secret_key)'
CSV;'
These serve: ERROR: syntax error at or near "CREDENTIALS"

reading a txt file: Permission denied - postgis

I get this error while trying to copy some data from a txt file to a table in a database, in postgis/postgres... (I am using windows 10).
ERROR: could not open file "C:\Users\Luchito\Desktop\output_1.txt" for reading: Permission denied
The file has all permission "on"... Any help?
Postgresql does not run with administrator privileges dans does not have access to your own user space most likely.
If you do a right click on the file, click Properties then Security, then Users (or Authenticated Users I am not sure) and check Full Control. It should allow Postgres to read the file

Copying CSV to Amazon RDS hosted Postgresql database

I have a database hosted using Amazon's RDS service and I am attempting to write a web service that will update said database. The problem I am having is that it will not let me use the COPY command as I get this error: "ERROR: must be superuser to COPY to or from a file". I am using the only user I have made for the database and I am fairly certain it has superuser access. I can, however, use PGAdmin's import tool to import the data which, when looking at the log, uses almost the exact same command as I do. The only difference is instead of the file path it has stdin. How can I fix this error?
You're using:
COPY tablename FROM 'filename';
this won't work - RDS has no idea what 'filename' is.
You must use the psql command's \copy, which copies from the local client, or PgAdmin-III's "import data" option.
The RDS manual covers this in more detail.
Workaround without using psql
1) Import data locally to a temporary table using simple copy command
2) Right click the table in the pgAdmin III object browser and select "Backup..."
3) Set the format to Plain and save the file as a .sql file
4) Click on the Dump Options #1 Tab and check Data
5) Click on the Dump Options #2 Tab and check Use Column Inserts and Use Insert Commands
6) Click Backup button
7) Now you can open the sql file and run it in your RDS server
Alternatively you can use the below command to generate the sql file
pg_dump --host localhost --port 5432 --username "postgres" --no-password --format plain --section data --inserts --column-inserts --file "C:\test\test.sql" --table "public.envelopes" "testdb"