rename database in psql - postgresql

Could anybody help me to rename database in postgresql from the Linux shell
ALTER DATABASE name RENAME TO newname
The above statement doesn't execute

This may be a stupidly obvious question. Are you running psql as the postgres user?
e.g.
$ sudo -u postgres psql
# alter database FOO rename to BAR;
# \q

Which version of postgresql? From the 8.1 Documentation:
ALTER DATABASE name RENAME TO newname;
Only the database owner or a superuser
can rename a database; non-superuser
owners must also have the CREATEDB
privilege. The current database cannot
be renamed. (Connect to a different
database if you need to do that.)

You might need privileges to rename db. Only db owner or super user can do that, owner also needs a createdb privilege.
Also the database you're connected to cannot be renamed, you need to connect to a different one

You cannot rename a database you are connected to. Make sure you are disconnected before changing the dbname.
In PGAdmin, you can just right-click on the database itself, go to properties, and rename it from there.
As others have pointed out, you may also try the command :
ALTER DATABASE (DB NAME) RENAME TO (NEW DB NAME);

Disconnect database (Ctrl + F2 in DataGrip)
And then:
$ psql -U postgres
postgres=# ALTER DATABASE db_a RENAME TO db_b;
GL

Below given are the steps for renaming the database in postgresql.
1) Right click on database and choose refresh.
2) Right click again and choose properties option.
3) Under the properties tab, you can change the name with the one that you desire.

Related

postgres main role is not postgres, how can I set it up to postgress

In terminal, when I write psql it login to "Coyr". I want "postgres" to be the main user and have all the attributes. How can I accomplish that?
I guess you ran initdb as user coyr without the -U option, and now you want to rename the bootstrap superuser.
That is easy:
drop the user postgres you created
create a new superuser maxi
connect as maxi and run
ALTER ROLE coyr RENAME TO postgres;
connect as postgres and
DROP ROLE maxi;
By renaming coyr will have lost its password, so if you need one, you have to set it again.

Fail to connect PostgreSQL DB from my Linux User

I am new to technologies , please do not judge my question too strong :).
I installed in My Ubuntu 18.04 PostgreSQL 10.7. To be able to enter my DB I need to enter the following commands from my terminal. sudo -u postgres psql.
Is there any shortened way where I can connect it from my Ubuntu User account. For example. if I input psql it will open database environment where I can type PostgreSQL commands.
Thank you.
Just execute this command in your terminal :
alias psql='sudo -u postgres psql'
So the next time, you input psql and execute, you will be in database environment.
I see two options:
1) Create alias for this command sudo -u postgres psql .
2) Go to psql and create new superuser and database for it:
CREATE ROLE username SUPERUSER;
ALTER ROLE username WITH LOGIN;
CREATE DATABASE username;
You shouldn't be using the superuser account for your normal database work. That is as if you were using root for everything in Linux.
You need to create a regular user with the privileges to create or modify tables in your database. This can be done by granting the user all privileges on the database (which is not the same as making that user a superuser) or make that user the owner of that database.
As documented in the manual psql tries to connect to a database with the name of the current Linux user and with a database user with the name of the current Linux user. So if you want to keep things simple create a user with your regular Linux user's name and an database that is owned by that user:
create user rob password 'somepassword';
create database rob owner = rob;
Assuming your Linux user is rob, then all you need to do is:
psql
and you are connected to a database where you can create and manage tables.
Depending on how you installed Postgres, you might need to adjust pg_hba.conf to allow rob to log in directly.
Again: please do NOT use the superuser account for your normal work.

How do I run and automate making a database with PSQL

I'm linking PSQL to a djangoREST api and I want it all to be automated, however I'm having trouble automating the creation of a database. Currently in my code I have this
initdb /usr/local/var/postgres, and while this creates a database I don't know how to change the owner and db name like you would when you would normally run
CREATE DATABASE <databasename>
I'm not sure if I understood your question fully. Two actions(rename/change owenr), you could do following way.
Rename database to new_databasename
ALTER DATABASE <databasename> RENAME TO <new_databasename>;
Change database owner to new_user
ALTER DATABASE <databasename> OWNER TO <new_databasename>;

Why command \dt gives - no relations found?

I have created a database in postgres. It has 3 empty tables. The table has user tom as its Superuser along with root. I am logged in as tom and connected to mydb database. But still the commands \d or \dt - gives no relations found.
Is there any alternate to SHOW TABLE in postgresql?
Could not find solution here
I get results from - \dt *.*.
It is not a problem with your search_path, it could be an issue with your schema permissions as described in the answer here. Check with \dn+ that the public schema indicates permissions for the postgres role, and if not, grant them with: GRANT ALL ON SCHEMA public TO public;
I solved my problem by using double quote e.g \d "Table_name". Because my table name is capitalized like Foo, Bar. Hope that could help someones.
You might not be connected to the right database.
The first command \c DATABASE_NAME, coming from the following comment on Reddit, did the trick for me.
Make sure you're connected to the correct database with \c . Using \l will list all databases regardless of which database you're connected to, but most commands are specific to the connected database.
Use \dt .* to list all tables with name matching in all schemas.
Assuming the table exists, it will tell you what schema it's in. Then you can query with SELECT * FROM .;.
If you want to avoid having to explicitly specify the schema name, make sure the schema that contains the table is in your path: SHOW search_path and SET search_path .
If you have different instances of postgres running on server on different port it helps when you connect to database with specific port psql -p 5432 databasename( or psql -p 5433 databasename etc.)
you must have not added a semicolon at the end that's what always happens to me I always forget my semicolon -> create table nameoftable()
This happened for me when I removed the default privileges for PUBLIC on the public schema (specifically the USAGE privilege).
I had the exact same issue. None of the answers above helped. I was able to see my table when I ran \dt .*. Then I realized I had to call explicitly state its schema when running SELECT statements.
So, if you run SELECT * FROM public.<your_tablename_goes_here>; it should work. I hope this helps!

What is a valid PostgreSQL database name?

I create a database with a hyphen in the middle of the name with createdb. That successfully creates the database, but within the psql interactive client, I get a syntax error if I try a command like this:
ALTER DATABASE my-database SET SCHEMA = myschema,public;
psql complains of a syntax error at or near "-"
Is there some documentation for what counts as a valid PostgreSQL database name?
Should I just underscores instead of hyphens?
The documentation you asked about is here:
http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Most people just stick to lowercase letters, numeric digits, and underscores -- to avoid typing the quotes all the time.
Try putting it in double quotes:
ALTER DATABASE "my-database" SET SCHEMA = myschema,public;
I faced one issue and above answers helped me. So sharing scenario on dbname
Scenario:I was tried to change database name using PG admin III. My database name was My_Database
running below queries failed:
ALTER DATABASE My_Database RENAME TO dba;
ALTER DATABASE [My_Database] RENAME TO dba;
ALTER DATABASE 'My_Database' RENAME TO dba;
Then i tried below and its successful
ALTER DATABASE "My_Database" RENAME TO dba;