I am trying to work out a way to perform a PostgreSQL pg_dump of a single DB via RHEL v6.10 command line. Now the system I am working on is that you have to shell into Bash 4.1 first before you get to PSQL. Now I can access the actual PSQL db using the following syntax from the command line which works fine:
'''sudo -u postgres -H --psql -pxxxxx -d db_name'''
If I enter the following syntax from the RHEL command line:
'''sudo su postgres'''
I end up in the bash-4.1 shell. When executing the following command from within the shell: bash-4.1$ pg_dump db_name > /tmp/my_database.sql
I am presented with the following error:
pg_dump: [archiver (db)] connection to database "db_name" failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I am expecting the db to be exported to /tmp/my_database.sql file. I have tried various combinations of commands from both RHEL and bash command lines but cannot achieve the desired outcome. Your assistance would be greatly appreciated, thank you.
Try this:
Connect as 'postgres' user
sudo -i -u postgres
Then run pg_dump, e.g.
pg_dump --blobs --file /your/backup/file/path/backupfilename.pgsql --dbname your_db_name
After that, you will find your backup file in /your/backup/file/path/backupfilename.pgsql
You can add many other options in pg_dump, See PostgreSQL Documentation
Related
i have a dump file on drive z (network drive)
im opening the psql from PgAdmin4
this is the command that im writeing:
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
and this is the error that im getting:
Invalid command \DB_BU. Try \? for help.
when im doing this:
psql -U postgres -d postgres -f i\ Z:\DB_BU\md_20220729.sql
Invalid command \DB_BU. Try ? for help.
and when im doing this:
psql -U postgres -d postgres -f "Z:\DB_BU\md_20220729.sql"
im not getting any error but also its not restoring the file. how can i restor the file?
You're trying to call psql from within psql or PGAdmin. Since
psql is a standalone program, not an SQL command you can run in PGAdmin SQL window or psql's own, internal meta-command you're getting the error
Invalid command \DB_BU. Try \? for help
indicating that there was an attempt to interpret your entire command as a SQL query or an internal command and that this attempt failed.
You can open "psql tool" from within PGAdmin but your command won't work there either because it's trying to call psql itself, with some command-line options, which you cannot do when you're already inside an interactive psql session. The command
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
can be used outside psql and PGAdmin, in your terminal, like zsh on Mac, sh/bash on Linux, cmd or PowerShell on Windows, where psql is installed and visible, along with your network path.
If you're able to open the psql tool window in PGAdmin, you can instead try and use an internal psql \i meta-command which is basically the same thing as the -f command-line option, but meant for use inside the psql session:
\i "Z:\DB_BU\md_20220729.sql"
I am setting up a website and am having some trouble restoring a database .dump file. I am using centos7, selinux, postgresql 9.4, and apache2.
This is my pg_hba.conf file.
This is the command I am trying to move the dump:
psql --single-transaction -U postgres db_name < dump_location
When I do this, I get the error:
Permission denied.
Am I missing something or is there someway I should alter my settings? Let me know if you need more information.
Thank you!
The operating system user you are running your shell as does not have permission to read the path dump_location.
Note that this is not necessarily the operating system user you run psql as. In a statement like:
sudo -u postgres psql mydb < /some/path
then /some/path is read as the current user, before sudo, not as user postgres, because it's the shell that performs the input redirection, not psql.
If, in the above example, you wanted to read the file as user postgres you would:
sudo -u postgres psql -f /some/path mydb
That instructs psql to open and read /some/path when it's started.
Just make sure that you are using correct database user and you have at least read permission on the dump file.
"psql -d -U postgres -f "
will work.
I have to create a postgresql database back up(only the schema) in Linux and restore it on a window machine. I backed up the database with option -E, but I was not able to restore it on the window machine.
Here is the command that I used to backup the database
pg_dump -s -E SQL_ASCII books > books.backup
below is the error message that I received when I tried to restore it.
C:/Program Files/PostgreSQL/9.3/bin\pg_restore.exe --host localhost --port 5432 --username "postgres" --dbname "books" --role "sa" --no-password --list "C:\progress_db\test1"
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
Am I supposed to use a different command or am I missing something? Your help is greatly appreciated.
The first lines in the official docs about restoring say:
The text files created by pg_dump are intended to be read in by the
psql program. The general command form to restore a dump is
psql dbname < infile
where infile is the file output by the pg_dump command
Or simply read the error message.
The option -E SQL_ASCII only sets the character encoding, it has nothing to do with the format of the dump. By default, pg_dump generates a text file that contains the SQL statements to regenerate the database; in this case, to restore the database you only need to execute the sql commands in the file, as in a psql terminal - that's why a simple psql dbname < mydump is the way to go.
pg_restore is to be used with the alternative "binary" -postgresql specific- format of pg_dump.
I try dropdb mydbname in shell. It do not give any error. But still when I call \l it is still there.
I logged into the postgres server using sudo -u postgres psql.
Other than my main concern I need to know how to go into the database other than just staying outside of it. (as a example if I want to list the tables)
In order to drop database you can use SQL command (but I do not understand why dropdb didn't work) DROP DATABASE mydbname:
sudo -u postgres psql -c "DROP DATABASE mydbname"
It would be good to check if database is not used:
select * from pg_stat_activity where datname = 'mydbname';
The sudo -u postgres psql connects to postgres database. You need to specify database: sudo -u postgres psql mydbname and then you can use metdata commands like \d, \dt, \dv, ...
When you say "shell" ... do you mean the psql shell, not the unix command line shell?
I'd say you're running into this issue:
Postgresql not creating db with “createdb” as superuser, yet not outputting errors
ie you're trying to use dropdb as a psql command, when it's a unix shell command. You're not getting an error because of this:
In psql, why do some commands have no effect?
You didn't terminate the command with a semicolon.
Are you missing the comma(;)? This command worked for me:
drop database <database_name>;
Server should be running, then:
dropdb <database name>
If server is not running, first try:
pg_ctl start -D <mylocal_db_path> -l <mylogfile.log>
In an NSIS installer, I use the following line to restore a PostgreSQL database from a file packaged with the installer.
ExecWait '$pg_restore_path --host 127.0.0.1 --no-password --port 5432 --username "postgres" --dbname "myDatabase" --verbose $EXEDIR/myDatabase.backup' $0
The command works but pg_restore seems to incorrectly set certain postgres sequences' current values (the current values either get reset to 1 or a number lower than that of the source) resulting in collisions. It seems to be a native bug with postgres but really not sure.
I have also tried replacing pg_restore with psql like this:
ExecWait '$psql_path -f "$EXEDIR/myDatabase.sql" myDatabase'
Which does not work; the terminal pops open and closes. Or like this:
ExecWait '$psql_path myDatabase < $EXEDIR/myDatabase.sql'
Which causes the error psql: warning: extra command-line argument "<" ignored
When I run the psql command manually from the command line it works like a charm, and sequences set properly. So my question is how to get the psql command working in NSIS with the file feeded < and avoiding the error. Failing that, any insight on using pg_restore differently that could work around the sequence issue?
Thanks
The following NSIS commands seem to have solved it.
ExecWait '$createdb_path -h "127.0.0.1" -p "5432" -U "postgres" -T "template1" --owner "user_owner" myDatabase'
ExecWait '$psql_path -f "$INSTDIR/myDatabase.sql" myDatabase user_owner'