This question already has answers here:
How to switch databases in psql?
(15 answers)
Closed 8 years ago.
I am quite new to postgresql and I am looking to timprove. I mostly use MySQL and from what I see already there are a few differences. Under mysql i would want to use or change my database (d1), i simply say the command
use d1;
What do I do under postgres?
I can view the databases and roles using commands such as:
select * from pg_roles;
select datname from pg_database;
Also are there comprehensive online resouces that I can point to?
Thanks
Psql uses short commands prefixed by a backslash to identify 'meta-commmands'; inputs that are for the command shell and not commands to be sent to the database.
You can get a list of these by typing \?
You can change databases in psql by using \c databasename
Here's the psql documentation for the newest release
Related
I have a SELECT query and I want to execute the result of this query as well. I think I need a variable to save it first and then execute. Some other answers to such question included commands from system bash, but I have a postgres running on my docker and I already entered my database in postgres. So how the command may look like?
This question already has answers here:
Export and import table dump (.sql) using pgAdmin
(6 answers)
Closed 1 year ago.
Let I first state that I am not a DBA-guy but I do have a question regarding restoring remote databases using PG Admin.
I have this PG Admin tool (v4.27) running in a Docker container and I use this portal to maintain two separate Postgress databases, both running in a Docker container as well. I installed PG Agent in both database containers and run scheduled daily backup's, defined via PG Admin and stored in the container of each corresponding databases. So far so good.
Now I want to restore one of these databases by using the latest daily backup file (*.sql), but the Restore Dialog of PG Admin only looks for files stored locally (the PG Admin container)?
Whatever I tried or searched for on the internet, to me it seems not possible to show a list of remote backup files in PG Admin or run manually a remote SQL file. Is this even possible in PG Admin? Running psql in the query editor is not possible (duh ...) and due to not finding the remote SQL-restore file I have no clue how to run this code within PG Admin on the remote corresponding database container.
The one and only solution so far I can think of, is scheduling a restore which has no calendar and should be triggered manually when needed, but it's not the prettiest solution.
Do I miss something or did I overlook the right documentation or have I created a silly, unmaintainable solution?
Thanks in advance for thinking along and kind regards,
Aad Dijksman
You cannot restore a plain format dump (an SQL script) with pgAdmin. You will have to use psql, the command line client.
COPY statements and data are mixed in such a dump, and that would make pgAdmin choke.
The solution by #Laurenz Albe points out that it is best to use the command line psql here, and that would be my first go-to.
However, if for whatever reason you don't have access to the command line and are only able to connect to this database via pgadmin, there is another solution which you can find here:
Export and import table dump (.sql) using pgAdmin
I recommend looking at the solution by Tomas Greif.
This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 2 years ago.
I downloaded postgresql installer version 11 from official website for windows.
when i enter psql and try to create database , database doesn't get created but it is creating by pgadmin4 with no problem
You are missing a semi-colon at the end of the line. You need to run:
CREATE DATABASE demo;
you miss semicolon on the end of command.
you should to write CREATE DATABASE demo;
psql by default allows multilines statements, so any statement should be finished by special character and it is semicolon ;
This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 6 years ago.
I have installed Postgres.app in Mac, and I have set the PATH variable to run the psql command.
Now I would like to create a new user and new database for a project that I'm creating. Nevertheless, when I execute a new command in terminal, it doesn't prompt me anything, and I don't know if my command was executed correctly.
Even if I write dummy words, it doesn't tell me anything:
Also, if I want to enter to the postgres user, it doesn't allow me, sending me the next error:
Does anyone know what is happening?.
Regards.
You have to append ; to each of your PostgreSQL - commands.
I am beginner to PostgreSQL.
I want to connect to another database from the query editor of Postgres - like the USE command of MySQL or MS SQL Server.
I found \c databasename by searching the Internet, but its runs only on psql. When I try it from the PostgreSQL query editor I get a syntax error.
I have to change the database by pgscripting. Does anyone know how to do it?
When you get a connection to PostgreSQL it is always to a particular database. To access a different database, you must get a new connection.
Using \c in psql closes the old connection and acquires a new one, using the specified database and/or credentials. You get a whole new back-end process and everything.
You must specify the database to use on connect; if you want to use psql for your script, you can use "\c name_database"
user_name=# CREATE DATABASE testdatabase;
user_name=# \c testdatabase
At this point you might see the following output
You are now connected to database "testdatabase" as user "user_name".
testdatabase=#
Notice how the prompt changes. Cheers, have just been hustling looking for this too, too little information on postgreSQL compared to MySQL and the rest in my view.
In pgAdmin you can also use
SET search_path TO your_db_name;
The basic problem while migrating from MySQL I faced was, I thought of the term database to be same in PostgreSQL also, but it is not. So if we are going to switch the database from our application or pgAdmin, the result would not be as expected.
As in my case, we have separate schemas (Considering PostgreSQL terminology here.) for each customer and separate admin schema. So in application, I have to switch between schemas.
For this, we can use the SET search_path command. This does switch the current schema to the specified schema name for the current session.
example:
SET search_path = different_schema_name;
This changes the current_schema to the specified schema for the session. To change it permanently, we have to make changes in postgresql.conf file.
Use this commad when first connect to psql
=# psql <databaseName> <usernamePostgresql>
set search_path = 'schema name here'
while connecting to the postgres, you have to opt for default database to connect. If you have nothing, you can use 'postgres' as default.
You can use dbeaver to connect to postgres. UI is good
PgAdmin 4, GUI Tool: Switching between databases
In the PgAdmin Browser on the left hand side, right click on the database you are willing to switch to.
Select a QueryTool from the drop down menu (or any other option that you need, I will stick with the QueryTool for now).
You will see the QueryTool in the PgAdmin window, and on top you will see the active database and the role name.
Now you can write queries against the chosen database.
You can open multiple QueryTools for multiple database, and work with them as you do with your graphical text editor.
In order to be sure that you are querying the proper database, issue the following query:
SELECT session_user, current_database();