I am trying to take backup of a table as sql file in postgres with below command.. the command is not executing properly.. can some one help ?
pg_dump -U postgres-a test_1 > test_1_dump.sql
Related
I was given a mydb.pg_dump file. I have created a new database test1 with pgAdmin. When I click right on the database and select Restore..., I can select my file after selecting "All files". But when I attempt to restore the database I immediately obtain this error:
pg_restore: error: input file appears to be a text format dump. Please
use psql.
Can I use pgAdmin to restore a .pg_dump file? How?
You cannot use pgAdmin for that, because in a plain-text dump COPY statements are mixed with COPY data.
You need psql for that, which fortunately is already installed on your machine:
psql -d mydb -U postgres -f mydb.pg_dump
"CMD.EXE /C " psql -h ipaddress -d dbname -u user -p password -c "\copy table to 'd:/bcptest/file.csv' with delimiter as '|'"
I have to execute this command in pentaho shell. But it is showing as parse error in script. By this command i have to copy data from a postgres table which is remote and save as .csv file in local.Please help.
You do not have to write any code inside pentaho for transferring
data.
You just have to create sample transformation using spoon, which will
have table input step create your database connection with the remote
server.
Then use text file output and store your result.
Understand the concept of ETL Tool before implementing anything.
I am a newbie and I have a postgresql large dump of type .sql and i want to import it. I am on Windows 10 and haven't been able to find solution.
I tried to restore using pgAdmin3 but it doesn't show .sql file while restoring. I also found few commands and tried them but nothing seems to work.
I also tried loading the datasource in IntelliJ DataGrid but it doesn't show the correct driver during the loading settings.
Can someone help?
First, figure out if the dump was created with pg_dumpall or pg_dump.
Dumps from pg_dumpall start with:
--
-- PostgreSQL database cluster dump
--
If the dump was created with pg_dump, find out if the -C option was used.
If yes, the dump will contain a line with a CREATE DATABASE statement.
To restore, use psql from the DOS box. I assume that psql is on your PATH.
A dump from pg_dumpall or pg_dump -C is restored with
psql -U postgres -d postgres -f dumpfile.sql
A dump from pg_dump without -C is restored with
psql -U postgres -d mydatabase -f dumpfile.sql
where mydatabase should be replaced with the name of the target database into which the dump should be restored.
I have a table (tst2) in a database (tweets) in PostgreSQL and I need to have a plain/text format file out of it, I was wondering if there is any possible solution with pg_dump ? something like :
pg_dump -t tst2 tweets -f plain >...
also if I am in a wrong way please let me know?!
There are a couple of ways to dump a table into a text file.
First, you can use pg_dump, as you intended. In that case you'll get a SQL script to restore the tables. Just fix your command a bit:
pg_dump -t tst2 -t tweets -F plain >...
Second, you can dump contents of a table with copy command. There are either SQL version of the command (files will be created on the server):
copy tst2 to 'tst2.txt';
copy tweets to 'tweets.txt';
Or client-side psql version (files will be created on your client computer):
\copy tst2 to 'tst2.txt';
\copy tweets to 'tweets.txt';
pg_dump works for me, though there is some clutter before and after the table (after all, dumps are supposed to be used to fill up the table at recovery time).
I'm not sure what you use the > operator for; the dump goes to the file `plane'.
Your error message would help, of course.
On the other hand, what's wrong with using psql with a .pgpass password file and setting the PGDATABASE, PGHOST, PGPORT, and PGUSER anvironment variables, e.g.:
export PGDATABASE=tweet
# PGHOST, PGPORT, and PGUSER as per your setup
psql -c 'select * from tst2'
I have a bunch of SQL scripts that create tables in the database. Each table is located in a separate file so that editing them is much easier.
I wanted to prepare a single SQL script that will create the full schema, create tables, insert test data and generate sequences for the tables.
I was able to do such thing for oracle database but I am having problems with postgres.
The thing is - I do not know how to run the table creating script from another script.
In oracle I do it using the following syntax:
##'path of the script related to the path of the currently running sql file'
And everything works like a charm.
In postgres I was trying to search for something alike and found this:
\ir 'relative path to the file'
Unfortunately when I run my main script I get the message:
No such file or directory.
The example call is here:
\ir './tables/map_user_groups.sql'
I use Postgres 9.3. I tried to run the script using psql:
psql -U postgres -h localhost -d postgres < "path to my main sql file"
The file executes fine except for the calling of those other scripts.
Does anybody know how to solve the problem ?
If something in the question is unclear - just let me know :)
Based on the answer It is possible to reference another SQL file from SQL script, on PostgreSQL, you can include another SQL's files just using the \i syntax. I just tested and is working good on PostgreSQL 9.6:
\i other_script.sql
SELECT * FROM table_1;
SELECT * FROM table_2;
By the #wildplasser comment:
psql -U postgres -h localhost -d postgres < "path to my main sql file"
From psql's perspective the main_sql file is just stdin, and stdin has no "filename". Use -f filename to submit a file with a name:
psql -U postgres -h localhost -d postgres -f filename.sql
How to run postgres sql script from another script?
Seems the same question as: How to import external sql scripts from a sql script in PostgreSQL?