Skip to content
SummaryCreate PDA accounts via invoke_signed with the PDA’s seeds. Only the owning program can sign for a PDA. Anchor’s init constraint automates PDA account creation.

​PDA Signing via invoke_signed

When a program needs to sign on behalf of a PDA during a CPI, it uses invoke_signed with the PDA’s seeds. The runtime verifies the seeds derive the expected PDA using the calling program’s ID, ensuring only the owning program can sign. For the full verification flow, see PDA Signing.
Security: Control what a PDA signature authorizesThe runtime checks that your program owns the PDA. It doesn’t check what the signature is used for. Inside invoke_signed, the callee can do anything that PDA’s signature allows, so:
  • Build signer seeds from validated accounts and stored state, never from raw instruction data. Caller-supplied seeds let the caller pick which of your PDAs signs.
  • Verify the program ID you invoke before signing (see CPI).
  • Re-derive the PDA and compare it with the account passed in, so the signature covers the account you intend.

​Create a PDA Account

Deriving a PDA and creating an account at a PDA are separate operations. You must explicitly create the account after deriving the address. To create an account at a PDA, the deriving program invokes the System Program’s create_account instruction via invoke_signed, passing the PDA’s seeds so the runtime can verify the program’s authority over that address. The example below uses the Anchor framework to create a new account with a program-derived address. The program includes a single initialize instruction to create the new account, which will store the user address and bump seed used to derive the PDA. To run this example locally with the Anchor CLI:
  1. Create a project with a single-file program and a Mocha test:
    Terminal
  2. Replace programs/pda-account/src/lib.rs with the program below and tests/pda-account.ts with the test.
  3. Point the program’s declare_id! at your project’s program ID:
    Terminal
    Without this step, anchor build only warns Program ID mismatch detected, and anchor test fails with an error such as DeclaredProgramIdMismatch (Anchor 1.1.2) or This program may not be used for executing instructions.
  4. Build the program and run the test:
    Terminal
Program
The init constraint tells Anchor to invoke the System Program to create a new account using the PDA as the address. The seeds used to create the PDA are:
  • The fixed string: “data”
  • The address of the user account provided in the instruction
  • The canonical bump seed
In this example, the bump constraint is not assigned a value, so Anchor will use find_program_address to derive the PDA and find the bump.
pda_account
The test file below contains a transaction that invokes the initialize instruction to create a new account with a program-derived address. The file contains code to derive the PDA. The example also shows how to fetch the new account that will be created.
Test
If you invoke the initialize instruction again with the same user address seed, the transaction will fail. This happens because an account already exists at the derived address.
Security: Reinitialization with init_if_neededAnchor’s init fails when the account exists, which is the behavior above. init_if_needed succeeds either way and runs your handler on an existing account. If the handler writes initial state unconditionally, a caller can reset a live account, for example overwriting its stored user or zeroing a balance. Use init where you can; with init_if_needed, check whether the account is already initialized before writing to it.