drop db in postgres - postgresql

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>

Related

psql faild to restor file from network drive

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"

How to run a postgres command: could not identify current directory

I am able to run psql by doing the following:
Davids-d david$ psql --u postgres
Password for user postgres:
psql (9.4.18)
Type "help" for help.
postgres=#
However, when I run the following command, I get an error:
Davids-iMac:datadocs david$ sudo -u postgres psql -f resources/postgresql/initdb.sql
could not identify current directory: Permission denied
What does this mean, and how would I resolve this? Note that I do have the following var set:
david$ echo $PGDATA
/Users/david/PostgreSQL/data/pg94
The issue is the sudo -u postgres.
Your shell is running as you, but you're running the command as the postgres user. It does not have permission to see the file or even be in the current directory.
We can eliminate psql from the equation by just trying to read the file as the postgres user with sudo -u postgres cat resources/postgresql/initdb.sql. You should get the same error.
There's two things you have to do...
cd to a directory that the postgres user can be in.
Put the file in a place the postgres user can access.
/tmp, for example.
Your command seems wrong, try this:
sudo psql -U postgres -f resources/postgresql/initdb.sql

psql.exe postgresql create database via CMD

I search to create a database via CMD, i success to create it using this command:
C:\>"C:\Program Files\PostgreSQL\9.3\bin\psql.exe" -U postgres template1
So when i excute this command i can Create my database like this:
template1=# CREATE DATABASE d_base;
My objectif is to create this database with just one line:
like this:
C:\>"C:\Program Files\PostgreSQL\9.3\bin\psql.exe" -U postgres template1 "CREATE DATABASE T;"
But this not work with me, it gave me this error:
i can solve this problem with creating a .bat Script but my objectif is to use just one line,
Is there any solution for that.
Thank you.
You get that error because template1 is not the last argument. You can use -d to specify the database.
Use the database postgres instead of template1.
Try this:
"C:\Program Files\PostgreSQL\9.3\bin\psql.exe" -U postgres -d postgres -c "CREATE DATABASE t"
https://www.postgresql.org/docs/9.3/static/app-psql.html
see --command=command section

Restoring .dump file - "Permission Denied"

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.

How to reindex Postgres 9.1.3 from the command line

I have a series of deletes and updates on a few tables in a Postgres database I manage. It has been suggested to schedule a reindex after the series of deletes as a solution to the 10 minute next-step update freezing infinitely (as it randomly does.) The DOS instructions provide this:
Usage:
reindexdb [OPTION]... [DBNAME]
Options:
-a, --all reindex all databases
-d, --dbname=DBNAME database to reindex
-e, --echo show the commands being sent to the server
-i, --index=INDEX recreate specific index only
-q, --quiet don't write any messages
-s, --system reindex system catalogs
-t, --table=TABLE reindex specific table only
--help show this help, then exit
--version output version information, then exit
Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as
-w, --no-password never prompt for password
-W, --password force password prompt
We have to use version 9.1.3 as this is the corporate standard.
I have tried every option I can think of but it won't take the command to reindex:
reindexdb.exe -U username=MyUserName -W MyPassword -t table=MyDatabase.MyTable
I've also tried
reindexdb.exe -U MyUserName -W MyPassword -t MyDatabase.MyTable
and
reindexdb.exe -U MyUserName -W MyPassword -t MyTable -d MyDatabase
...but they all end with the error:
reindexdb: too many command-line arguments (first is "-t")
Does anyone have a working sample that would be able to clarify what the right syntax is?
Remove MyPassword from your arguments, and enter it in when Postgres prompts you for it.
-W simply causes Postgres to prompt for the password; it doesn't accept the password itself. You should never specify passwords on the command line, as it's usually logged.
If you need to run it non-interactively, either set the PGPASSWORD environment variable or create a pgpass file.
This did it:
reindexdb.exe -d MyDatabase -U postgres -t MyTable
As #Colonel Thirty Two and #Erwin Brandstetter noted, removing the password entirely is possible through %APPDATA%\postgresql\pgpass.conf
Any of these can be forced by adding the keyword FORCE after the command
Recreate a single index, myindex:
REINDEX INDEX myindex
Recreate all indices in a table, mytable:
REINDEX TABLE mytable
Recreate all indices in schema public:
REINDEX SCHEMA public
Recreate all indices in database postgres:
REINDEX DATABASE postgres
Recreate all indices on system catalogs in database postgres:
REINDEX SYSTEM postgres
link